C#.NET “在没有任何数据时进行无效的读取尝试”
SqlConnectionconn=newSqlConnection("server=;uid=;pwd=;database=");conn.Open();Label1....
SqlConnection conn = new SqlConnection("server=;uid=;pwd=;database=");
conn.Open();
Label1.Text = Request.Params["ID"].ToString();
int id = Convert.ToInt32(Label1.Text);
string sql = "select NewsContent from News_Content where ID='" + id + "'";
SqlCommand mycmd = new SqlCommand(sql, conn);
SqlDataReader dr = mycmd.ExecuteReader();
Label2.Text=dr["Content"].ToString();
Label1里的id能读出来,但是Label2里用SQL语言查询就报错了
SQL里字段名也是Content,打错了
SqlConnection连接当然有了,只不过我省略了而已
数据库的连接应该没有问题,conn的状态是已打开 展开
conn.Open();
Label1.Text = Request.Params["ID"].ToString();
int id = Convert.ToInt32(Label1.Text);
string sql = "select NewsContent from News_Content where ID='" + id + "'";
SqlCommand mycmd = new SqlCommand(sql, conn);
SqlDataReader dr = mycmd.ExecuteReader();
Label2.Text=dr["Content"].ToString();
Label1里的id能读出来,但是Label2里用SQL语言查询就报错了
SQL里字段名也是Content,打错了
SqlConnection连接当然有了,只不过我省略了而已
数据库的连接应该没有问题,conn的状态是已打开 展开
展开全部
SqlConnection conn = new SqlConnection("server=;uid=;pwd=;database=");
conn.Open();
Label1.Text = Request.Params["ID"].ToString();
int id = Convert.ToInt32(Label1.Text);
string sql = "select NewsContent from News_Content where ID='" + id + "'";
SqlCommand mycmd = new SqlCommand(sql, conn);
SqlDataReader dr = mycmd.ExecuteReader();
if (dr.Read())
{
Label2.Text=dr["Content"].ToString();
}
conn.Open();
Label1.Text = Request.Params["ID"].ToString();
int id = Convert.ToInt32(Label1.Text);
string sql = "select NewsContent from News_Content where ID='" + id + "'";
SqlCommand mycmd = new SqlCommand(sql, conn);
SqlDataReader dr = mycmd.ExecuteReader();
if (dr.Read())
{
Label2.Text=dr["Content"].ToString();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询