如何对null值进行处理呢?imaxid = (int)command.ExecuteScalar();
如下,如果sql执行后,返回的是null,如何处理呢?
正常下,下面的sql返回的是 一个 int 值,但是如果取不到记录时,
imaxid = (int)command.ExecuteScalar(); 就会报错,
如何进行容错处理呢?
//取交书地点 orderid
public int GetOrderId(string strkehu)
{
int imaxid;
strsql = " select max(orderid) as maxid from cp_交书地点 where 客户编码='" + strkehu + "'";
command = new SqlCommand(strsql, mydb.conn);
imaxid = (int)command.ExecuteScalar();
return imaxid;
}
如下,如果sql执行后,返回的是null,如何处理呢?
正常下,下面的sql返回的是 一个 int 值,但是如果取不到记录时,
imaxid = (int)command.ExecuteScalar(); 就会报错,
如何进行容错处理呢?
//取交书地点 orderid
public int GetOrderId(string strkehu)
{
int imaxid;
strsql = " select max(orderid) as maxid from cp_交书地点 where 客户编码='" + strkehu + "'";
command = new SqlCommand(strsql, mydb.conn);
imaxid = (int)command.ExecuteScalar();
return imaxid;
}
解决方案 »
- winform,gridview在最后一行的图片列显示X图片,怎么显示正确呢。
- Jmail接收外部邮件代码
- Win7、Windows Server2003 Web服务连接不上服务器?什么办
- 正则式引用中有bug吗? 正则式的高手进来看一来.
- 写了个接收页面的程序,主要是通过http传输值给一个send.asp这个页面,但是send.asp接收的中文数据出现乱码,请各位高手进来指点一下
- 请教关于mstts(ms text-to-speech)中语音的语言问题
- 请大家帮忙看看这段代码~~!
- 非常奇怪的问题,你见过吗?
- 怎么获取string类型部分字符?
- 数据绑定的问题
- 关于清楚控件值
- 高分求解中!!C#窗体设计中UserControl设计使用问题!!!!!!!!!!
if(x!=null)
imaxid = (int)x;
//取交书地点 orderid
public int GetOrderId(string strkehu)
{
strsql = " select max(orderid) as maxid from cp_交书地点 where 客户编码='" + strkehu + "'";
command = new SqlCommand(strsql, mydb.conn);
int imaxid = command.ExecuteScalar()!=null?(int)command.ExecuteScalar():0;
return imaxid;
}
也可以用 int? 而不是int,不过一般不必要。
public int GetOrderId(string strkehu)
{
int? imaxid = null;
strsql = " select max(orderid) as maxid from cp_交书地点 where 客户编码='" + strkehu + "'";
command = new SqlCommand(strsql, mydb.conn);
imaxid = (int?)command.ExecuteScalar();
return imaxid == null ? -1 : (int)imaxid;
}