C#从SQL Server数据库中选择

Max*_*ner 0 c# sql-server

如果记录存在,我想在我的C#应用​​程序中进行额外的控制.

我有以下代码 - 但它仍然返回-1的结果,即使该记录确实存在于SQL Server数据库中.

有人可以帮我弄这个吗?我添加了 - >出错的地方

private void btnVerwijderen_Click(object sender, RoutedEventArgs e)
{
    if (autonrTextBox.Text == "")
    {
        MessageBox.Show("Waarschuwing u kunt geen auto verwijderen indien er GEEN autonr is ingevuld");
    }
    else
    {
    --> SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-RSEBNR7;Initial Catalog=AudiDealer;Integrated Security=True");
    --> string check = "SELECT autonr FROM auto WHERE autonr =@autonr";

    --> SqlCommand command1 = new SqlCommand(check, con);
    --> command1.Parameters.AddWithValue("@autonr", autonrTextBox.Text);

        con.Open();
        int auto = command1.ExecuteNonQuery();
        con.Close();

--> X - 1   MessageBox.Show(auto.ToString());

        if (auto > 0)
        {
            try
            {
                con.Open();

                using (SqlCommand command = new SqlCommand("DELETE FROM auto WHERE autonr =" + autonrTextBox.Text, con))
                {
                    command.ExecuteNonQuery();
                }

                con.Close();
            }
            catch (SystemException ex)
            {
                MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
            }
        }
        else
        {
            MessageBox.Show("Het opgegeven autonr komt niet voor in de database. controleer deze.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Joe*_*orn 6

ExecuteNonQuery()方法不像您认为的那样工作.此方法的返回值是更改的行数,而不是结果集中的任何内容.SELECT查询不会更改行,因此-1是预期结果.0行将暗示WHERE子句在UPDATE,DELETE或INSERT中不匹配任何行.-1用于表示不同的情况......不是更改行或回滚的语句.请查看方法文档中的备注部分.

您想要使用该ExecuteScalar()方法.

int auto = -1;
using (var con = new SqlConnection(@"Data Source=DESKTOP-RSEBNR7;Initial Catalog=AudiDealer;Integrated Security=True"))
using (var cmd = new SqlCommand("SELECT autonr FROM auto WHERE autonr =@autonr", con))
{
    cmd.Parameters.Add("@autonr", SqlDbType.Int).Value = int.Parse(autonrTextBox.Text);
    con.Open();
    auto = (int)cmd.ExecuteScalar();
}
Run Code Online (Sandbox Code Playgroud)

最后......为什么在删除之前检查?这太浪费了.只需发出DELETE语句即可.首先不需要SELECT.你的try/catch和if()检查已经处理了记录不存在的情况.

int autonr = 0;
if (!int.TryParse(autonrTextBox.Text, autonr))
{
    MessageBox.Show("Waarschuwing u kunt geen auto verwijderen indien er GEEN autonr is ingevuld");
}
else
{
    try 
    {
        using (var con = new SqlConnection(@"Data Source=DESKTOP-RSEBNR7;Initial Catalog=AudiDealer;Integrated Security=True"))
        using (var cmd = new SqlCommand("DELETE FROM auto WHERE autonr = @autonr;", con))
        {
            cmd.Parameters.Add("@autonr", SqlDbType.Int).Value = autonr;
            con.Open();
            int result = cmd.ExecuteNonQuery();
            if (result <= 0)
            {
                 MessageBox.Show("Het opgegeven autonr komt niet voor in de database. controleer deze.");
            }
        }
    }
    catch (SystemException ex)
    {
        MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
    }
}
Run Code Online (Sandbox Code Playgroud)