C#.NET “在没有任何数据时进行无效的读取尝试”

来源:百度知道 编辑:UC知道 时间:2024/06/04 07:01:32
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的状态是已打开

....

SqlConnection conn = new SqlConnection("server=;uid=;pwd=;database="); 这句里面什么都没有指定 肯定报错啊
如果数据库在本地的话应该这样写
server=.;(或者是local)
uid=**;(这里是用户名一般默认的是sa)
pwd=**;(这里是用户名对应的密码)
database=**(这里是要连接的数据库名)

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();
}