SqlDataReader 判空问题?

zhaooh 2008-12-05 11:27:55
 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;


上述sql语句搜索无果,但thisReader却不等于null,使我retrun 0 语句无法执行,导致 count = thisReader.GetInt32(0);报错,如何在搜索无果的情况下,执行return 0;语句啊?
...全文
662 43 打赏 收藏 转发到动态 举报
写回复
用AI写文章
43 条回复
切换为时间正序
请发表友善的回复…
发表回复
marsowen123 2012-09-06
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 的回复:]
C# code
SqlDataReader dr = new SqlDataReader();
if (dr.Read()) //或if (dr.HasRows)
{
//有内容
}
else
{
//无内容
}
[/Quote]

正确答案!!没看到楼上已经有人回答出来!
marsowen123 2012-09-06
  • 打赏
  • 举报
回复
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;
}
  • 打赏
  • 举报
回复
用 Bool 型判断?……
就两种情况:读到和没读到……
是吧?简单吧……晕……
Terran5 2009-03-11
  • 打赏
  • 举报
回复
吧你的thisReader == null改成


if (thisReader.GetSchemaTable().Rows.Count == 0)
{
return 0;
}
zhaoqiliang527 2009-03-11
  • 打赏
  • 举报
回复
using (SqlDataReader dr = cmdSql.ExecuteReader())
{
if (dr!=null)
{
dt.Load(dr);
}
dr.Close();
}
pt1314917 2009-03-11
  • 打赏
  • 举报
回复

//应该是这样吧?
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;

「已注销」 2009-03-11
  • 打赏
  • 举报
回复
            catch (Exception ex)
{
Response.Write(ex.ToString());
Response.End();
}
finally
public_private 2008-12-06
  • 打赏
  • 举报
回复
顶7楼
llsen 2008-12-05
  • 打赏
  • 举报
回复
sqldatareader有一个isnull函数
你看下
或者是isnul
llsen 2008-12-05
  • 打赏
  • 举报
回复
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绑定值
llsen 2008-12-05
  • 打赏
  • 举报
回复
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绑定值
niitnanfeng 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 XIEWH 的回复:]
C# codeif(thisReader.Read())
{
count=thisReader.GetInt32(0);
}else{return0;
}
[/Quote]
有颜色的问题你看见下面那回帖的编辑器了没?A下面有个红色的那个A,选中你的要加颜色的文字点A就可以了。
claymore1114 2008-12-05
  • 打赏
  • 举报
回复
if(thisRead.HasRows)//记录集是否为空
{
while(thisRead.Read())

{
.....
}
}
wesleyluo 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 XIEWH 的回复:]
C# code if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}
else
{
return 0;
}
[/Quote]
这句简洁方便。
Tiger_ldy 2008-12-05
  • 打赏
  • 举报
回复
reader.HasRow
路人乙e 2008-12-05
  • 打赏
  • 举报
回复
最好不要直接 return 0;这样的数据连接就不会关闭
else
{
count = 0;
}
路人乙e 2008-12-05
  • 打赏
  • 举报
回复
if (thisReader== null)
{
return 0;
}
if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}
==》
if (thisReader.Read())
{
count = thisReader.GetInt32(0);
}
else
{
return 0;
}

patrickpan 2008-12-05
  • 打赏
  • 举报
回复
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;

XIEWH 2008-12-05
  • 打赏
  • 举报
回复
看错。
没结果if (thisReader.Read())还能进去?
HDNGO 2008-12-05
  • 打赏
  • 举报
回复
HasRows
加载更多回复(23)

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧