为什么我的ExecuteNonQuery总是返回一个大于0的值

时间:2015-12-27 04:35:53

标签: c# sql executenonquery

您好我现在编码的搜索框将从sql获取数据,并检查是否有任何此类数据,但是当我运行我的代码时

public void checkValues()
    {
        using (connection = new SqlConnection(connectionString))
        using (SqlCommand cmdMovname = new SqlCommand("select Film_name from product where Film_name like'%the value here is not in the sql data %'", connection))
        {
            connection.Open();

            if (cmdMovname.ExecuteNonQuery() != 0)
            {
                SetValues();
                find = "Search result for \"the value here is not in the sql data\"";
            }
            else if (cmdMovname.ExecuteNonQuery() == 0)
            {
                find = "Sorry , no search result for \"the value here is not in the sql data\"";
            }

        }
    }

find的字符串值将始终为“搜索结果为”,此处的值不在sql data \“”中 有人可以帮助我这个原因我是新手使用SQL数据库

1 个答案:

答案 0 :(得分:2)

请尝试使用ExecuteReader。您正在尝试执行查询,但使用ExecuteNonQuery函数,在这种情况下将返回-1。

Reference