0

我正在使用 VS2010 和 SqlServer2008-r2 在 C# 中设计一个基于窗口的应用程序。我是

使用基于服务的数据库(.mdf),其中有一个包含四个字段的表,如果我存储

表中的数据并关闭应用程序并重新运行应用程序数据丢失。

为什么会这样以及如何摆脱它。

我正在使用以下例程进行保存

private void Save(object sender, EventArgs e)
        {
            Program.connection.Close();

            bool k = srchpreventry();

            try
            {
                if (k)
                {

                 string query = " update orderform set Enrolment_Expected = " + textBox2.Text + ", Stock_on_Hand=" + textBox3.Text + ", Number_Required = "+ textBox4.Text + " where Name = '" + textBox1.Text + "';";
               SqlCommand cmd = new SqlCommand(query, Program.connection);

                cmd.ExecuteNonQuery();

                Program.connection.Close();
            }


            else
            {
               // Program.connection.Open();

                string query = "insert into orderform(Name,Enrolment_Expected,Stock_on_Hand,Number_Required) values('" + textBox1.Text + "', '" + textBox2.Text + "', ' " + textBox3.Text + "',' " + textBox4.Text + "')";

                SqlCommand cmd = new SqlCommand(query, Program.connection);

                cmd.ExecuteNonQuery();

                Program.connection.Close();
            }

        }

        catch (Exception ae)
        {
            string str = ae.ToString();
            MessageBox.Show(str);
        }

        finally
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            textBox3.Enabled = false;
            textBox4.Enabled = false;
            Program.connection.Close();
        }

    }    



public bool srchpreventry()
        {

            Program.connection.Open();

            string query = " Select name from orderform where Name = '" + textBox1.Text + "';";

            SqlCommand cmd = new SqlCommand(query, Program.connection);

            SqlDataReader dtr = cmd.ExecuteReader();

            if (dtr.Read() == true)
            {
                dtr.Close();
                return true;
            }
            else
            {
                dtr.Close();
                return false;
            }
        }



private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Program.connection.Close();
            Program.connection.Open();



            string query = " Select * from orderform where Name = '"  + textBox1.Text + "';";

            SqlCommand cmd = new SqlCommand(query, Program.connection);

            SqlDataReader dtr = cmd.ExecuteReader();

            if (dtr.Read() == true)
            {
                textBox2.Text = dtr[1].ToString();
                textBox3.Text = dtr[2].ToString();//GetString(2);
               textBox4.Text = dtr[3].ToString();
            }

            else
            {
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";

            }


        }



public static  SqlConnection connection = null;
       static string appath = Library_Records.Program.app_path;

string connectionstring = string.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;User Instance=True", appath);

static string dbfiles = null;
        internal static string app_path
        {
            get { return dbfiles = "|Datadirectory|\\records.mdf"; }
        }


/*******************datagrid code********************/
Program.connection.Open();
                    string query = "select * from orderform";
                    SqlDataAdapter MyDA = new SqlDataAdapter();
                    MyDA.SelectCommand = new SqlCommand(query, Program.connection);
                    DataTable table = new DataTable();
                    MyDA.Fill(table);

                    BindingSource bSource = new BindingSource();
                    bSource.DataSource = table;

                    dataGridView1.DataSource = bSource;
4

3 回答 3

0

检查您是否可以增加列中允许的字符,例如 nvarchar(max) 因为现在它可能是 nvarchar(200) - 这只是一个示例

于 2012-06-27T10:29:47.283 回答
0

在 Visual Studio 中?

您不是每次开始调试时都让 VIsual Studio 再次加载相同的空数据库吗?

并关闭应用程序并重新运行应用程序数据丢失。

要么有人忽略插入时抛出的错误,要么不提交事务,要么 tvisal studio 每次启动时都将相同的 rdatabase 模板占用到目录中。

于 2012-06-27T10:45:09.450 回答
-2

我强烈(强调强烈)建议您开始使用存储过程(在代码中或在数据库中),但除此之外..您不启动事务或类似的东西?或将 Program.Connection 类代码发布到问题中。

于 2012-06-27T10:47:29.030 回答