0

我一直在从事与数据库(.mdf)相关的项目。我使用 C# 在 Visual Studio 中创建了一些 Windows 窗体。基本上,这些表单一起工作以存储、更新和删除我创建的基于服务的数据库中的数据。问题是当我构建项目时,它构建得很好,没有错误。它也按预期将文本框提供的数据插入到 datagridview 中。但是一旦我停止当前调试,然后再次重新运行,之前提供的所有数据都会从 datagridview 中丢失!!我不明白为什么会这样。任何人请帮助我。我对这些东西完全陌生.. 一点指导将不胜感激。

当我以前将 MySQL 用于相同目的时,更新的数据将永久存储到数据库中,但由于我从 MySQL 迁移到 SQL Server 的基于服务的数据库,我得到了如此令人困惑的错误。

    ......
    void loadData()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["baikalpik_bidhut_sewaConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("SELECT SNo,Customer_ID, Citizenship_No, Name, Subscription_Date, Phone_No, Location,Locality,Bulbs,Deposit,Monthly_Charge FROM customerinformation;", con);
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = cmd;
            DataTable dt = new DataTable();
            adp.Fill(dt);
            dataGridViewCustomerInformation.DataSource = dt;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void buttonAdd_Click(object sender, EventArgs e)
    {
        try
        {
            float m_chrg = Convert.ToInt64(textBoxBulbs.Text)*500;
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["baikalpik_bidhut_sewaConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand("INSERT INTO customerinformation(SNo,Customer_ID,Citizenship_No,Name,Subscription_Date,Location,Locality,Bulbs,Deposit,Phone_No,Monthly_Charge) values('" + textBoxSNo.Text + "','" + textBoxCustomerID.Text + "','" + textBoxCitizenshipNumber.Text + "','" + textBoxName.Text + "','" + textBoxSubscriptionDate.Text + "','" + textBoxLocation.Text + "','" + textBoxLocality.Text + "','" + textBoxBulbs.Text + "','" + textBoxDeposit.Text + "','" + textBoxPhoneNumber.Text + "','" + m_chrg + "')", con);

            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            dt = new DataTable();
            dt.Load(reader);
            con.Close();
            dataGridViewCustomerInformation.DataSource = dt;

            loadData();
            MessageBox.Show("Entry Added!");
            fillListbox();

            textBoxSNo.Clear();
            textBoxBulbs.Clear();
            textBoxCitizenshipNumber.Clear();
            textBoxCustomerID.Clear();
            textBoxDeposit.Clear();
            textBoxLocality.Clear();
            textBoxLocation.Clear();
            textBoxPhoneNumber.Clear();
            textBoxName.Clear();
            textBoxSubscriptionDate.Clear();



        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
4

1 回答 1

0

If you have your MDF listed in your project files then the property Copy To Output Directory handles how your MDF file is copied to the output directory (BIN\DEBUG or BIN\RELEASE).

If this property is set to Copy Always, then when you run your program a fresh copy of your database file is copied from the project folder to the output effectively destroying every data that you have inserted, modified, deleted in the previous run of your program.

The best solution to this dilemma is to add the MDF file as a permanent database inside your current install of Sql Server and adjust your connection string. And of course set the property to Copy Never

或者,您可以设置为Copy if newer. 这将允许更改服务器资源管理器中的数据库架构,并让 Visual Studio IDE 仅在您进行更改后复制新结构。

更新但重要与您的实际问题无关,但您的插入查询是一个严重的问题。不要使用字符串连接来构建 sql 命令文本。特别是如果部分文本来自用户输入。您可能会遇到语法错误(如果有人在文本框中放置单引号),或者最糟糕的是 Sql 注入问题(请参阅此处以获得有趣的解释
找到插入搜索的好示例'parametrized query'

于 2013-05-04T17:34:18.510 回答