SQLREADER With Search
本文关键字:Search With SQLREADER | 更新日期: 2023-09-27 18:17:41
//Creating Data Reader Components
string str_ClientID;
string str_Occupation;
string strCompany;
public void Read_Out_Clients(string company)
{
//Specify the parameter search criteria
sqlComLoad.Parameters["@company"].Value = company;
sqlCon.Open(); // open database connection
// create database reader to read information from database
SqlDataReader objReader = sqlComLoad.ExecuteReader();
// retrieve
// information from database
//<--This Errors saying there is not data in the data base
//Suppose to return the ClientID field data All field names are correct
str_ClientID = Convert.ToString(objReader["ClientID"]);
//suppose to return the Occupation field data
str_Occupation = Convert.ToString(objReader["Occupation"]);
objReader.Close(); // close data reader connection
sqlCon.Close(); // close database connection
}
//This is the returning value to the method
private void cboCompany_SelectedIndexChanged(object sender, EventArgs e)
{
strCompany = cboCompany.SelectedItem.ToString();
if(strCompany != "")
{
//Read out companys selected related fields from clients
Read_Out_Clients(strCompany);
//Add those related fields to specifics
cboClientID.Items.Add(str_ClientID); <--this is what the reader is supposed to return
txtOccupation.Text = str_Occupation;
}
}
任何帮助使这个阅读器简单将是伟大的!谢谢大家假定该代码读取数据字段company与查询搜索匹配的数据字段标准它与Access数据库工作,但由于某种原因,它不与SQL查询工作。它应该工作,我测试了它在SQL查询执行器,它返回的数据,但在应用程序级别的错误,当我运行阅读器,它说,"无效尝试读取时,没有数据存在";但是有数据它返回的数据在SQL查询执行器,所以我很困惑,请帮助再次感谢。
在使用SqlDataReader(或任何由DbDataReder派生的类实例)之前,您需要调用方法
if(objReader.Read())
{
... read the fields
}
并检查它是否返回true。
只有在此之后,您的objReader
定位在您的查询检索的第一个记录上。