SqlDataReader 判空问题? thisReader肯定不等于null,即使搜出来是0条数据你可以用thisReader.Fill(ds);然后判断ds.Tables[0].Rows.Count; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]); SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection); myConnection.Open(); SqlDataReader thisReader = thisCommand.ExecuteReader(); int count = 0; if (!thisReader.HasRows)//在搜索不到数据时,thisReader并不是null,而是无数据。 { return 0; } if (thisReader.Read()) { count = thisReader.GetInt32(0); } thisReader.Close(); myConnection.Close(); return count; if (thisReader.Read()) { count = thisReader.GetInt32(0); } else { return 0; } 看错。没结果if (thisReader.Read())还能进去? SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]); SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection); myConnection.Open(); SqlDataReader thisReader = thisCommand.ExecuteReader(); int count = 0; if (thisReader.GetSchemaTable().Rows.Count == 0) //修改这行就可以了。 { return 0; } if (thisReader.Read()) { count = thisReader.GetInt32(0); } thisReader.Close(); myConnection.Close(); return count; if (thisReader== null){return 0;}if (thisReader.Read()){count = thisReader.GetInt32(0);}==》if (thisReader.Read()){count = thisReader.GetInt32(0);}else{return 0;} 最好不要直接 return 0;这样的数据连接就不会关闭else{count = 0;} if(thisRead.HasRows)//记录集是否为空{ while(thisRead.Read()) { ..... }} 有颜色的问题你看见下面那回帖的编辑器了没?A下面有个红色的那个A,选中你的要加颜色的文字点A就可以了。 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]); SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection); myConnection.Open(); SqlDataReader thisReader = thisCommand.ExecuteReader(); int count = 0; if (thisReader== null) { return 0; } if (thisReader.Read()) { count = thisReader.GetInt32(0); } thisReader.Close(); myConnection.Close(); return count;sqldatareader是一行一行取数据怎么能fill(ds)那好像也没有用if(this.reader.read())这样用的啊这这去一条,第一条,没有得到这个的用途这个是这么用的 ListItem newItem = new ListItem(); ListBox1.Items.Clear(); while (reader.Read()) { newItem = new ListItem(); newItem.Text = reader["Creater"].ToString().Trim(); newItem.Value = reader["HospitalID"].ToString().Trim(); ListBox1.Items.Add(newItem); }用reader逐行给listbox绑定值 sqldatareader是一行一行取数据 怎么能fill(ds)那 好像也没有用if(this.reader.read())这样用的啊 这这去一条,第一条,没有得到这个的用途 这个是这么用的 ListItem newItem = new ListItem(); ListBox1.Items.Clear(); while (reader.Read()) { newItem = new ListItem(); newItem.Text = reader["Creater"].ToString().Trim(); newItem.Value = reader["HospitalID"].ToString().Trim(); ListBox1.Items.Add(newItem); } 用reader逐行给listbox绑定值 sqldatareader有一个isnull函数你看下或者是isnul SqlDataReader dr = new SqlCommand("command string", new SqlConnection("connection string")).ExecuteReader(); while (dr.Read()) { } 我都做过实验了,reader.hasrows都为ture啊 你要注意 你的这个 select 语句 如果没有错误的话 是肯定会返回一条记录的..在数据没有合适数据的时候 也就是 结果是count 为0 hasrows是肯定会为true的不能使用这个判断if (thisReader.Read())//这里要记住 一但成功执行了Read()方法数据就开始读取到第一行了.如果要读取多条数据 使用while语句{ count = thisReader.GetInt32(0);}else return 0; if(thisRead.HasRows){ while(thisRead.Read()) { ..... }} if(thisReader.isDBnull(0)==false)//判断是否读出的为Null 答案是这样的 if (thisReader.Read()) { count = thisReader.GetInt32(0); //这出错是因为你查到的数据,第一列值,非Int32类型吧. }如果想在这边处理的话,,就在这加个try () {} catch () {} 如下.if (thisReader.Read()) {try (){ count = thisReader.GetInt32(0); //这出错是因为你查到的数据,第一列值,非Int32类型吧.}catch (Exception e ){count = 0 ;MessageBox.Show (e.Message); // 这个只是会提示异常信息.你不喜欢的话,随你去掉好了!} } if (thisReader.Read()) 能进去就说明不为空啊. 你的thisReader.GetInt32(0); 是用来取thisReader当前行 第一列里面的int32数据. 支持一下!SqlDataReader······ SqlDataReader dr = new SqlDataReader(); if (dr.Read()) //或if (dr.HasRows) { //有内容 } else { //无内容 } HasRows属性,或者thisReader.Read() catch (Exception ex) { Response.Write(ex.ToString()); Response.End(); } finally //应该是这样吧?SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]); SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection); myConnection.Open(); SqlDataReader thisReader = thisCommand.ExecuteReader(); int count = 0; if (thisReader== null) { return 0; thisReader.Close(); myConnection.Close(); } if (thisReader.Read()) { count = Int32.Parse(thisReader["count"].ToString()); } thisReader.Close(); myConnection.Close(); return count; using (SqlDataReader dr = cmdSql.ExecuteReader()) { if (dr!=null) { dt.Load(dr); } dr.Close(); } 吧你的thisReader == null改成 if (thisReader.GetSchemaTable().Rows.Count == 0) { return 0; } 用 Bool 型判断?……就两种情况:读到和没读到……是吧?简单吧……晕…… reader.read() = true,表示读到的有内容;相反没有读到内容if (thisReader== null) { return 0; } if (thisReader.Read()) { count = thisReader.GetInt32(0); }可以修改为if thisReader.reader(){ count = thisReader.GetInt32(0);}else {return 0;} 新手求教,子窗口获得父窗口数据问题 大家帮帮忙 高分再开一贴,问题没解决,急救 关于局域网文件夹传输的速度问题 在关键字‘table’附近有语法错误 关于Dataset的GetXml的问题。 c#能写成c/s架构的winform程序么?要是能怎么发布呀? dataGrid中按下鼠标右边后如何将当前列选定 AcitveX控件问题,高手请进,分不够在加,急,在线等 急!!LISTVIEW的问题?? 怎么让c#程序开机启动(把c#程序写进启动项)最好有代码示例, 谢谢了, 感激中 如何读取图像到byte[]
SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection); myConnection.Open();
SqlDataReader thisReader = thisCommand.ExecuteReader();
int count = 0; if (!thisReader.HasRows)//在搜索不到数据时,thisReader并不是null,而是无数据。
{
return 0;
}
if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}
thisReader.Close();
myConnection.Close();
return count;
{
count = thisReader.GetInt32(0);
}
else
{
return 0;
}
没结果if (thisReader.Read())还能进去?
SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection); myConnection.Open();
SqlDataReader thisReader = thisCommand.ExecuteReader();
int count = 0; if (thisReader.GetSchemaTable().Rows.Count == 0) //修改这行就可以了。
{
return 0;
}
if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}
thisReader.Close();
myConnection.Close();
return count;
{
return 0;
}
if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}
==》
if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}
else
{
return 0;
}
else
{
count = 0;
}
{
while(thisRead.Read())
{
.....
}
}
SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection); myConnection.Open();
SqlDataReader thisReader = thisCommand.ExecuteReader();
int count = 0; if (thisReader== null)
{
return 0;
}
if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}
thisReader.Close();
myConnection.Close();
return count;
sqldatareader是一行一行取数据
怎么能fill(ds)那好像也没有用if(this.reader.read())这样用的啊
这这去一条,第一条,没有得到这个的用途这个是这么用的
ListItem newItem = new ListItem();
ListBox1.Items.Clear();
while (reader.Read()) {
newItem = new ListItem();
newItem.Text = reader["Creater"].ToString().Trim();
newItem.Value = reader["HospitalID"].ToString().Trim();
ListBox1.Items.Add(newItem);
}
用reader逐行给listbox绑定值
怎么能fill(ds)那 好像也没有用if(this.reader.read())这样用的啊
这这去一条,第一条,没有得到这个的用途 这个是这么用的
ListItem newItem = new ListItem();
ListBox1.Items.Clear();
while (reader.Read()) {
newItem = new ListItem();
newItem.Text = reader["Creater"].ToString().Trim();
newItem.Value = reader["HospitalID"].ToString().Trim();
ListBox1.Items.Add(newItem);
}
用reader逐行给listbox绑定值
你看下
或者是isnul
SqlDataReader dr = new SqlCommand("command string", new SqlConnection("connection string")).ExecuteReader();
while (dr.Read())
{ }
在数据没有合适数据的时候 也就是 结果是count 为0
hasrows是肯定会为true的
不能使用这个判断
if (thisReader.Read())//这里要记住 一但成功执行了Read()方法数据就开始读取到第一行了.如果要读取多条数据 使用while语句
{
count = thisReader.GetInt32(0);
}
else
return 0;
{
while(thisRead.Read())
{
.....
}
}
{
count = thisReader.GetInt32(0); //这出错是因为你查到的数据,第一列值,非Int32类型吧.
}
如果想在这边处理的话,,
就在这加个try () {} catch () {} 如下.
if (thisReader.Read())
{
try ()
{
count = thisReader.GetInt32(0); //这出错是因为你查到的数据,第一列值,非Int32类型吧.
}
catch (Exception e )
{
count = 0 ;
MessageBox.Show (e.Message); // 这个只是会提示异常信息.你不喜欢的话,随你去掉好了!
} }
你的thisReader.GetInt32(0); 是用来取thisReader当前行 第一列里面的int32数据.
if (dr.Read()) //或if (dr.HasRows)
{
//有内容
}
else
{
//无内容
}
{
Response.Write(ex.ToString());
Response.End();
}
finally
//应该是这样吧?
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection); myConnection.Open();
SqlDataReader thisReader = thisCommand.ExecuteReader();
int count = 0; if (thisReader== null)
{
return 0;
thisReader.Close();
myConnection.Close();
}
if (thisReader.Read())
{
count = Int32.Parse(thisReader["count"].ToString());
}
thisReader.Close();
myConnection.Close();
return count;
{
if (dr!=null)
{
dt.Load(dr);
}
dr.Close();
}
if (thisReader.GetSchemaTable().Rows.Count == 0)
{
return 0;
}
就两种情况:读到和没读到……
是吧?简单吧……晕……
if (thisReader== null)
{
return 0;
}
if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}可以修改为
if thisReader.reader()
{ count = thisReader.GetInt32(0);
}
else
{
return 0;
}