我正在运行SQL查询并返回DataSet
- 我希望迭代DataSet
中的信息并将值分配给局部变量。我已经确认我的DataSet
会返回行,但我的变量由于某种原因没有被分配。有人可以查看我的语法并协助解决这个问题的原因吗?
protected void GetSomeData()
{
string employeeid = this.txtemployeeid.Text;
DataSet empinfo = new DataSet();
empinfo = RunSqlQuery(employeeid);
this.txtemployeefirstname = empinfo.Tables[0].Rows[0]["employeefirstname"].ToString());
this.txtemployeelastname = empinfo.Tables[0].Rows[1]["employeelastname"].ToString());
this.txtemployeeaddress = empinfo.Tables[0].Rows[2]["employeeaddress"].ToString());
this.txt.employeephone = empinfo.Tables[0].Rows[3]["employeephone"].ToString());
}
public DataSet RunSqlQuery(string employeeid)
{
string sqlQuery = "Select employeefirstname, employeelastname, employeeaddress, employeephone from abcd where employeeid = "+employeeid+" and employeeid is not null";
try
{
connectionString = System.Configuration.ConfigurationManager.AppSettings[connectionString].ToString();
sqlDatabaseConnection = new SqlConnection(connectionString);
sqlCommand = new SqlCommand(sqlQuery, sqlDatabaseConnection);
sqlDatabaseConnection.Open();
sqlCommand.CommandTimeout = 0;
dataSet = new DataSet();
sqlDataAdapter = new SqlDataAdapter(sqlCommand);
sqlDataAdapter.Fill(dataSet, "Data");
return dataSet;
}
catch (Exception exception)
{
throw exception;
}
finally
{
sqlDatabaseConnection.Close();
sqlCommand.Dispose();
sqlDataAdapter.Dispose();
}
}
修改
通过未分配的变量,我的意思是当我单步执行代码时,似乎从数据库返回的值没有填充到empinfo.Tables[0].Rows[0]["employeefirstname"].ToString());
中,它们始终为空。
答案 0 :(得分:1)
您将值分配给文本框(至少,我假设txtemployeefirstname是一个文本框)。我认为你的意思是txtemployeefirstname.text?