简体   繁体   English

在Windows窗体中更新SQL Server Express数据库时出错

[英]Error in updating a SQL Server Express database in windows forms

This is my problem: 这是我的问题:

I am creating an application in C# using Visual Studio 2010. In one of the modules of the application, I have to update an existing table and insert into another table. 我正在使用Visual Studio 2010在C#中创建一个应用程序。在该应用程序的模块之一中,我必须更新现有表并将其插入到另一个表中。 Both tables are created. 两个表均已创建。

Problem is when I don't close the total application, it updates and inserts into the tables perfectly, but when I close the application and open it again all the values are reset. 问题是当我不关闭整个应用程序时,它会完美更新并完美插入表中,但是当我关闭应用程序并再次打开它时,所有值都将重置。

I think that only the data set is being updated and not the actual database. 我认为只有数据集在更新,而不是实际的数据库。

I want to know how to actually update the database. 我想知道如何实际更新数据库。

My code looks somewhat like this: 我的代码看起来像这样:

    System.Data.SqlClient.SqlConnection con;                      
    System.Data.SqlClient.SqlCommand cmd,cmd1,cmd2;

    private void Form3_Load(object sender, EventArgs e)
    {
        con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\pharmacy.mdf;Integrated Security=True;User Instance=True";
        con.Open();
        MessageBox.Show("Connection successful!");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        cmd1 = new System.Data.SqlClient.SqlCommand("Select med_id from stock where med_id= " + textBox1.Text + ";", con);
        object result = cmd.ExecuteScalar();
        if (result == null)
        {
            cmd = new System.Data.SqlClient.SqlCommand("insert into stock values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "');", con);
            cmd.ExecuteNonQuery();
        }
        else
        {
            cmd2 = new System.Data.SqlClient.SqlCommand("alter table stock set current_stock = (select current_stock from stock where med_id=" + textBox1.Text + ";) + "+ textBox8.Text +" where med_id="+textBox1.Text+";",con);
         }
            cmd2.ExecuteNonQuery();     
    }

These steps worked for me, I'm using Visual Studio Express 2013 for Windows Desktop: 这些步骤对我有用,我正在使用Windows Studio的Visual Studio Express 2013:

Step 1: Go to database explorer, right click database name (mdf file), select modify connections, select Browse, within your project folder, go into the bin folder, then go into the Debug folder, select the mdf file in the debug folder, select Test Connection to ensure connection succeeds, then click OK 步骤1:转到数据库资源管理器,右键单击数据库名称(mdf文件),选择修改连接,选择浏览,在您的项目文件夹中,进入bin文件夹,然后进入Debug文件夹,在debug文件夹中选择mdf文件,选择测试连接以确保连接成功,然后单击确定

Next, go to properties of the database (mdf file), click the connection string box in the properties explorer menu, copy the data source file path in the connection string box 接下来,转到数据库的属性(mdf文件),单击属性浏览器菜单中的连接字符串框,在连接字符串框中复制数据源文件路径

Step 2: In the Solutions Explorer box, double click 'Properties', go to Settings option, Insert Name - (can be anything you want, I named mine EmployeeConnString) Insert Type - In the dropdown box, select connection string Insert Scope - in the dropdown box, select application Insert Value - right-click paste (previously copied file path), press enter You can exit this tab 步骤2:在“解决方案资源管理器”框中,双击“属性”,转到“设置”选项,插入名称-(可以输入任意名称,我命名为EmployeeConnString)插入类型-在下拉框中,选择连接字符串插入作用域-在在下拉框中,选择应用程序插入值-右键单击粘贴(先前复制的文件路径),按Enter键,您可以退出此标签

Step 3: Go back to the solutions explorer box and select the database file in your vs project(mdf file), Go to the properties explorer window to the copy to output directory option, change "Copy Always" to "Copy Never" or "Do Not Copy", 步骤3:返回解决方案资源管理器框,然后在vs项目(mdf文件)中选择数据库文件,转到属性资源管理器窗口中的复制到输出目录选项,将“始终复制”更改为“从不复制”或“不要抄袭”,

These steps worked for me and now everything I enter updates correctly, as well as when i re-run the application, the updated data shows in the new form box. 这些步骤对我有用,现在我输入的所有内容均正确更新,并且当我重新运行该应用程序时,更新的数据将显示在新表单框中。

As usual - upon running, Visual Studio makes a copy of your database file. 像往常一样-运行时,Visual Studio会复制数据库文件。 Then, you work with that copy, you update records there. 然后,您使用该副本,在那里更新记录。 When you invoke the application again, the original file is copied over the one you made changes into. 当您再次调用该应用程序时,原始文件将被复制进行更改的文件上。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM