gpt4 book ai didi

C# - SELECT 查询受影响的行数始终为 -1

转载 作者:行者123 更新时间:2023-11-30 18:51:45 28 4
gpt4 key购买 nike

我有这段代码,它总是返回-1。我有三个表(一张图片更具启发性): enter image description here

我想看看该行是否已经在 ReservationDetails 表中,如果不插入它。

try
{
SqlConnection conn = new SqlConnection...
SqlCommand slct = new SqlCommand("SELECT * FROM ReservationDetails WHERE rID=@rID AND RNumber=@RNumber", conn);
slct.Parameters.AddWithValue("@rID", (int)comboBox1.SelectedValue);
slct.Parameters.AddWithValue("@RNumber", dataGridView1.SelectedRows[0].Cells[0].Value);

int noRows;//counts if we already have the entry in the table
conn.Open();
noRows = slct.ExecuteNonQuery();
conn.Close();
MessageBox.Show("The result of select="+noRows);

if (noRows ==0) //we can insert the new row

最佳答案

你读过SqlCommand.ExecuteNonQuery的文档了吗?

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. 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. If a rollback occurs, the return value is also -1.

您的查询是 SELECT。

关于C# - SELECT 查询受影响的行数始终为 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10053037/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com