简体   繁体   English

查询始终在C#中返回-1

[英]Query is always returning -1 in C#

I am using following code to check if record exist. 我正在使用以下代码来检查记录是否存在。

SqlCommand check_Campaign_Name = new SqlCommand("SELECT * FROM Campaign_Summary  WHERE ([Compaign_Name] = @user) ", conn);
check_Campaign_Name.Parameters.AddWithValue("@user", txtBox_LastClick_Campaign.Text);

int CampaignExist = check_Campaign_Name.ExecuteNonQuery();

richTextBox1.Text = CampaignExist.ToString();

But I am always getting -1 in the integer CampaignExist . 但是我总是在整数CampaignExist得到-1 Don't know where I am doing wrong. 不知道我在哪里做错了。

ExecuteNonQuery is not supposed to return the number of rows SELECTED , but the number of rows modified by an INSERT/UPDATE/DELETE command. ExecuteNonQuery不应返回SELECTED的行数,而应返回INSERT / UPDATE / DELETE命令修改的行数。 You should use a SqlDataReader and check with its property HasRows or use an aggregate function like COUNT and ExecuteScalar 您应该使用SqlDataReader并检查其属性HasRows或使用诸如COUNTExecuteScalar的聚合函数
(Probably the best choiche if you want to just retrieve the number of rows) (如果只想获取行数,可能是最好的选择)

SqlCommand check_Campaign_Name = new SqlCommand("SELECT COUNT(*) FROM Campaign_Summary " + 
                                 "WHERE Compaign_Name = @user", conn);
check_Campaign_Name.Parameters.AddWithValue("@user", txtBox_LastClick_Campaign.Text);
int rowCount = Convert.ToInt32(check_Campaign_Name.ExecuteScalar());

However, if you want only to know if the row exists or not, then the COUNT approach is considered less efficient than using the EXISTS statement. 但是,如果只想知道该行是否存在,则认为COUNT方法的效率不如使用EXISTS语句。

string cmdText = @"IF EXISTS (SELECT Compaign_Name FROM Campaign_Summary 
                   WHERE Compaign_Name = @user)
                   SELECT 1 ELSE SELECT O";
SqlCommand check_Campaign_Name = new SqlCommand(cmdText, conn);
int rowExists = Convert.ToInt32(check_Campaign_Name.ExecuteScalar());

This second approach just allows to know if there are rows that fits the WHERE statement or not. 第二种方法只是允许知道是否有适合WHERE语句的行。
So it is not exactly like COUNT(*) where you get the exact number of rows. 因此,它与获得确切行数的COUNT(*)不完全相同。

Use executescalar method and count on query 使用executescalar方法并依靠查询

SqlCommand check_Campaign_Name = new SqlCommand("SELECT COUNT(1) FROM Campaign_Summary  WHERE ([Compaign_Name] = @user) ", conn);
check_Campaign_Name.Parameters.AddWithValue("@user", txtBox_LastClick_Campaign.Text);

int CampaignExist = Convert.ToInt32(check_Campaign_Name.ExecuteScalar());

richTextBox1.Text = CampaignExist.ToString();

From MSDN , MSDN

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. 对于UPDATE,INSERT和DELETE语句,返回值是该命令影响的行数。 When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. 当要插入或更新的表上存在触发器时,返回值包括受插入或更新操作影响的行数以及受一个或多个触发器影响的行数。 For all other types of statements, the return value is -1. 对于所有其他类型的语句,返回值为-1。 If a rollback occurs, the return value is also -1. 如果发生回滚,则返回值也为-1。

As your query is a SELECT and not one of the mentioned UPDATE , INSERT or DELETE s, it will always return -1 . 由于您的查询是SELECT而不是上述UPDATEINSERTDELETE ,因此它将始终返回-1

Try with Execute scalar MSDN 尝试执行scalar MSDN

int CampaignExist = Convert.ToInt(check_Campaign_Name.ExecuteScalar());

SqlCommand check_Campaign_Name = new SqlCommand("SELECT COUNT(1) FROM Campaign_Summary  WHERE ([Compaign_Name] = @user) ", conn);
check_Campaign_Name.Parameters.AddWithValue("@user", txtBox_LastClick_Campaign.Text);

int CampaignExist = Convert.ToInt32(check_Campaign_Name.ExecuteScalar());

richTextBox1.Text = CampaignExist.ToString();

ExecuteNonQuery is not used to retrieve results ExecuteNonQuery不用于检索结果

You can use one of the following methods: 您可以使用以下方法之一:

  1. SqlCommand.ExecuteScalar (I would prefer this) SqlCommand.ExecuteScalar (我希望这样做)

  2. DataAdapter.Fill

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM