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

时间:2013-05-04 17:09:30

标签: c# database-connection

我一直在研究与数据库相关的项目(.mdf)。我使用C#在visual studio中创建了一些窗体。基本上,这些表单一起用于存储,更新和删除我创建的基于服务的数据库中的数据。 问题是当我构建项目时,它构建良好,没有错误。它也会按照预期将从文本框提供的数据插入到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);
        }
    }

1 个答案:

答案 0 :(得分:0)

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

如果此属性设置为Copy Always,那么当您运行程序时,数据库文件的新副本将从项目文件夹复制到输出,从而有效地销毁您已插入,修改,删除的每个数据上一次运行你的程序。

解决这一难题的最佳方法是将MDF文件添加为当前Sql Server安装中的永久数据库,并调整连接字符串。当然,将属性设置为Copy Never

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

更新但重要与您的实际问题无关,但您的插入查询是一个严重的问题。不要使用字符串连接来构建sql命令文本。特别是如果部分文本来自用户输入。你可能会遇到语法错误(如果有人在文本框中放置一个引号)或最糟糕的是,Sql Injection问题(see here for a funny explanation
要查找插入搜索'parametrized query'

的好例子
相关问题