我应该怎么做才能放置一些IF,因为有时我的查询没有任何答案 [英] what should I do to put some IF for sometimes that my query does not have any answer

查看:68
本文介绍了我应该怎么做才能放置一些IF,因为有时我的查询没有任何答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写此sql查询:

I write this sql query:

SqlCommand cmd2 = new SqlCommand();
       cmd2.Connection = new SqlConnection(Class1.CnnStr);
       cmd2.CommandText = "SELECT MAX(Code) FROM Table WHERE Number=@Number ";
       cmd2.Connection.Open();
       cmd2.Parameters.AddWithValue("@Number", Hidden_txt.Text);
       cmd2.ExecuteNonQuery();



我想放一些(IF),例如:



and I want to put some (IF) such as:

if (cmd2.ExecuteScalar()=="Null")
{....}


有时我的查询没有答案,但是没有用.


for sometimes that my query does not have answer but it doesn''t work. what should I do to put some IF for sometimes that my query does not have any answer?

推荐答案

您可以像这样编辑查询:
You can edit your query like this :
SELECT MAX(ISNULL(Code,0)) FROM Table WHERE Number=@Number


请改用try catch块...
在try子句中执行SQL Command和"connection.open"语句...

另外,由于execute non query返回受影响的行数,因此您可以检查返回的行数是否大于0


PS.使用存储过程,而不是直接从代码本身直接触发查询
Use a try catch block instead...
Execute your SQL Command and "connection.open" statements in the try clause...

Also since execute non query returns number of rows affected, you can check if the number of rows returned is greater than 0


PS. Use stored Procedures rather than fire the query directly from the code itself



您可以尝试

Hi,
you can try by

int result=-1;
result=cmd2.ExecuteNonQuery();

if(result != -1)
{

}



它会正常工作.因为ExecuteNonQuery返回一个表示否"的"int"值.受影响的行数.因此,您可以尝试此...



it will work proper. Because ExecuteNonQuery returns an "int" value indicating the no. of rows affected.so you can try this...


这篇关于我应该怎么做才能放置一些IF,因为有时我的查询没有任何答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆