Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL Server更新在C中不起作用_C#_Sql_Sql Server_Winforms - Fatal编程技术网

C# SQL Server更新在C中不起作用

C# SQL Server更新在C中不起作用,c#,sql,sql-server,winforms,C#,Sql,Sql Server,Winforms,我在项目中链接了一个SQL Server.mdf数据库文件。然后我创建了这个类: public class Manager { public libraryDataSet ds = new libraryDataSet(); private String strConnection = Properties.Settings.Default.libraryConnectionString.ToString(); public void GetBooks() {

我在项目中链接了一个SQL Server.mdf数据库文件。然后我创建了这个类:

public class Manager
{
    public libraryDataSet ds = new libraryDataSet();
    private String strConnection = Properties.Settings.Default.libraryConnectionString.ToString();

    public void GetBooks()
    {
        using (SqlConnection con = new SqlConnection(strConnection))
        {
            SqlDataAdapter adapter = new SqlDataAdapter("select * from book ", con);
            adapter.Fill(ds.book);
        }
    }

    public void UpdateBook(string status, string bookName, int bookNumber)
    {
        SqlCommand command;
        StringBuilder strUpdate = new StringBuilder();

        strUpdate.AppendFormat("update book set status='{0}' where bookName like '{1}%' and bookNumber={2} ", status, bookName, bookNumber);

        try
        {
            using (SqlConnection con = new SqlConnection(strConnection))
            {
                command = new SqlCommand(strUpdate.ToString(), con);
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        catch (Exception ex) { Console.WriteLine(ex); }
    }
}
在一个表单中,我使用GetBooks方法和LINQ查询,用图书填充DataGridView,按名称搜索

然后,我尝试使用UpdateBook方法更新一本书的状态,但它不起作用。没有例外


我卡住了,我做错了什么?

您忘记在GetBooks方法中打开连接。另外请看,听起来您的where子句有一些问题,可能是因为操作员没有按预期工作。请按照@Grantwiney的建议将您的输入添加到问题中,请确认数据库中的记录尚未更新。它可能已更新,而您只是没有用更新的值重新填充DataGridView。请在ExecuteOnQuery行上放置一个断点,并准确检查正在执行的查询。试着手动运行那个查询,看看它是否正常工作。@Givi我认为并没有必要打开连接,因为使用了。