简体   繁体   English

为什么我的C#代码没有更新SQL Server数据库,尽管我得到了正确的受影响行数

[英]Why doesn't my C# code update the SQL Server database although I get the correct number of affected rows

I created the following code: 我创建了以下代码:

public static bool setHeadword(int id, string headword)
{       
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\pms.mdf;Integrated Security=True";
    conn.Open();

    SqlCommand command = new SqlCommand("UPDATE headwords SET Headword = @headword WHERE Id = @id", conn);

    command.Parameters.AddWithValue("@headword", headword);
    command.Parameters.AddWithValue("@id", id);

    int result = command.ExecuteNonQuery();
    conn.Close();

    return true;
}

But the code doesn't work because the value in the database doesn't change. 但是代码不起作用,因为数据库中的值没有更改。 If I run the code manually in the database the change takes place. 如果我在数据库中手动运行代码,则会发生更改。 But it won't work with C#. 但是它不适用于C#。

Also the result variable are holding the right number of affected rows (1 in this case). 同样,结果变量保留正确数量的受影响的行(在这种情况下为1)。

I'm not sure I have to flush the changes or something else. 我不确定是否必须清除更改或其他内容。

Thanks for your help and best regards Franz 感谢您的帮助和最诚挚的问候Franz

static void Update(int id, string headword)
{
   try
   {    
   //You should create connectionString with correct details otherwise fail connection
    string connectionString =
        "server=.;" +
        "initial catalog=employee;" +
        "user id=sa;" +
        "password=123";
    using (SqlConnection conn =
        new SqlConnection(connectionString))
    {
        conn.Open();
        using (SqlCommand cmd =
            new SqlCommand("UPDATE headwords SET Headword=@headword" +
                " WHERE Id=@Id", conn))
        {
            cmd.Parameters.AddWithValue("@Id", id);
            cmd.Parameters.AddWithValue("@headword", headword);

            int rows = cmd.ExecuteNonQuery();


        }
    }
   }
catch (SqlException ex)
{
    //Handle sql Exception
}

} }

暂无
暂无

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

相关问题 C#SQL,获取受UPDATE影响的行 - C# SQL, Get rows affected by UPDATE 当我单击更新按钮以更新SQL C#中的数据库值时,所有行都受到相同值的影响 - When i click the update button to update database values in SQL C# All Rows Are Affected by the same value ExecuteNonQuery()不会在C#和SQL Server数据库中返回受影响的行 - ExecuteNonQuery() does not return the affected rows in C# and SQL Server database 为什么我的代码不更新数据库? - Why Doesn't my Code Update The database? 如果在 c# 中运行 sql 脚本时遇到 sql 异常,如何获取受影响的行数 - How to get the number of rows affected, if it hits sql exception while running the sql script in c# 如何在SQL Server或ASP.NET C中强制执行特定数量或范围的受影响的行# - How to enforce a certain number or range of affected rows in SQL Server or ASP.NET C# 我正在运行 web 应用程序并想使用 C# 代码更新我的 SQL 服务器用户配置文件数据库,但无论我做什么我都无法让它工作 - I am running a web app and want to update my SQL Server user profile database with C# code but no matter what I do I cannot get it to work C#ADO.NET UPDATE不会更改数据库记录,但仍会返回1个受影响的行 - C# ADO.NET UPDATE doesn't change database record, but still returns 1 affected row(s) 为什么我的 SQL 更新查询不起作用? (行不受影响) - Why isn't my SQL Update query working? (the row is not affected) C#程序不会从DataGridView更新SQL数据库 - C# program doesn't update SQL database from DataGridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM