问题:SqlReader of Read()不工作
用户行动:
方案行动:
错误列表:
数据库:
这是我的代码:
private void swipe_button_Click(object sender, EventArgs e)
{
String ID_givenbyUSER = IDtxtBox.Text;
SqlConnection sqlConn = null;
sqlConn = new SqlConnection("Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=ABC_SchoolDB;Integrated Security=True");
sqlConn.Open();
SqlCommand cmd = new SqlCommand("select Student_Name from dbo.Sheet@Attendance where Serial_Id=" + " ' " + ID_givenbyUSER + " ' ", sqlConn);
SqlDataReader sqlReader = cmd.ExecuteReader();
richTxtBox.Clear();
richTxtBox.AppendText("Hi buddy "); //This line works
while (sqlReader.Read())
{
richTxtBox.AppendText("Hi buddy "); //But,Its not work
pwdbox.Text = (sqlReader["Student_Name"].ToString()); //Its not work too
}
if (sqlConn != null)
{
sqlConn.Close();
sqlConn = null;
}
}
}发布于 2014-02-20 17:30:47
我想你的问题是:
SqlCommand cmd = new SqlCommand("select Student_Name from dbo.Sheet@Attendance where Serial_Id=" + " ' " + ID_givenbyUSER + " ' ", sqlConn);试一试:
SqlCommand cmd = new SqlCommand("select Student_Name from dbo.Sheet@Attendance where Serial_Id='" + ID_givenbyUSER + "'", sqlConn);注意到你连接单引号的部分了吗?它的周围有空格,所以您的查询看起来如下:
Where Serial_Id= ' Name ' 用户提供的值前面的空间可能导致查询不返回任何行。
https://stackoverflow.com/questions/21915117
复制相似问题