我有一个选择状态,我使用 SQL 的 ExecuteNonQuery() 属性返回受影响的行数。但是,这始终返回 -1。我做了研究并意识到它是 SQL 的 SETNOCOUN ON 属性的结果。请问我如何从查询中删除它,因为我没有使用存储过程?下面是我的代码。
protected void txtName_Changed(object sender, EventArgs e)
{
string sql = "";
using (SqlCommand cmd = connection.CreateCommand())
{
sql = "SET NOCOUNT OFF;SELECT * FROM [CLIENT] WHERE cname = '" + txtName.Text + "'";
cmd.Connection = connection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
connection.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0 )
{
txtName.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD5E53");
txtName.BorderColor = System.Drawing.ColorTranslator.FromHtml("#CD4A4A");
lblError.Text = "Client Exist.";
lblError.Visible = true;
lblError.Enabled = true;
btnInsertClient.Enabled = false;
pnlAddEdit_ModalPopupExtender.Show();
// return;
}
else
{
txtName.BackColor = System.Drawing.ColorTranslator.FromHtml("#F2F0E1");
txtName.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
lblError.Text = "";
lblError.Visible = false;
lblError.Enabled = false;
btnInsertClient.Enabled = true;
pnlAddEdit_ModalPopupExtender.Show();
}
connection.Close();
}
}