在VS2010 Ultimate中使用基于服务的数据库

本文关键字:服务 数据库 于服务 Ultimate VS2010 | 更新日期: 2023-09-27 18:12:19

我一直在做一个与数据库(.mdf)相关的项目。我在visual studio中使用c#创建了一些windows窗体。基本上,这些表单一起工作,从我创建的基于服务的数据库中存储、更新和删除数据。问题是当我构建项目时,它构建得很好,没有错误。它按预期将从文本框提供的数据插入到数据视图中。但是只要我停止当前的调试,然后再次重新运行,以前提供的所有数据都从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);
        }
    }

在VS2010 Ultimate中使用基于服务的数据库

如果您在项目文件中列出了MDF,则属性Copy To Output Directory处理如何将MDF文件复制到输出目录(BIN'DEBUG或BIN'RELEASE)。

如果此属性设置为Copy Always,那么当您运行程序时,数据库文件的新副本将从项目文件夹复制到输出中,有效地破坏您在先前运行程序时插入,修改,删除的所有数据。

这个困境的最佳解决方案是在当前安装的Sql Server中添加MDF文件作为永久数据库,并调整连接字符串。当然还要将属性设置为Copy Never

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

UPDATE BUT IMPORTANT与你的实际问题无关,但是你的插入查询是一个严重的问题。不要使用字符串连接来构建sql命令文本。特别是当部分文本来自用户输入时。你可能会遇到语法错误(如果有人在文本框中放置了一个单引号),或者更糟糕的是,Sql注入问题(查看这里的有趣解释)
要查找插入的好例子,请搜索'parametrized query'