这是我的代码..它表明DR1是一个变量,并且正在被用作方法,请一些人为我解释这个,我该如何解决它我需要从数据库中获取数据并发布它在标签上
SqlConnection sqlConnection1 = new SqlConnection("sql connection");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from request where reqNo = '" + lbl_reqNoV.Text + "', sqlConnection1";
//cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
SqlDataReader DR1;
DR1 = cmd.ExecuteReader();
DR1.Read();
if (DR1.HasRows)
{
lbl_reqNoV.Text = DR1("ReqNo");
}
答案 0 :(得分:1)
这一行是你的问题:
lbl_reqNoV.Text = DR1("ReqNo");
DR1
是一个变量。但DR1()
使用方法调用的语法。
你可能意味着这个:
lbl_reqNoV.Text = DR1["ReqNo"];
或者可能
lbl_reqNoV.Text = DR1.GetString("ReqNo");
答案 1 :(得分:0)
当你得到错误1时不能隐式地将类型'object'转换为'string'。存在显式转换(您是否缺少演员表?)E:\ Planning application \ Purchase \ Default.aspx.cs 49 35立即购买此错误
您必须使用ToString()获取从datareader读取的值。请参阅以下代码以供参考
SqlConnection sqlConnection1 = new SqlConnection("Data Source=[Server];Initial Catalog=MyDB;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from request where reqNo = '" + lbl_reqNoV.Text + "'";
//cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
SqlDataReader DR1;
DR1 = cmd.ExecuteReader();
if (DR1.HasRows)
{
DR1.Read();
lbl_reqNoV.Text = DR1["ReqNo"].ToString();
}
sqlConnection1.Close();