使用文本框添加后,Access数据库中缺少数据

时间:2016-07-08 18:57:55

标签: c# datagridview datagrid

所以,我正在尝试创建一个记录保存应用程序,用户可以通过C#文本框放置数据,并且可以通过DataGrid访问它们。当我通过文本框插入数据并单击提交(Unesi)时,数据插入顺利,所有数据在数据网格中都可见。但是,重新启动应用程序后,即使我有绑定功能,DataGridView和Access数据库也都缺少数据。

这是我的Binding功能:

 private void BindDataGrid()
    {
        using (myConnection = new OleDbConnection(myConnectionString))
        {
            myConnection.Open();

            using (myOleDbDataAdapter = new OleDbDataAdapter())
            {
                DataSet myDataSet = new DataSet();

                myOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT * FROM SVIRKE", myConnection);
                myOleDbDataAdapter.Fill(myDataSet, "Svirke");

                bsSvirke.DataSource = myDataSet;
                bsSvirke.DataMember = "Svirke";
            }
            myConnection.Close();
        }
       // ClearTextBoxes();
    }

这是我的提交按钮代码:

 private void txtSave_Click(object sender, EventArgs e)
    {
        //if (!String.IsNullOrEmpty(txtCompanyName.Text) && !String.IsNullOrEmpty(txtPhone.Text))
        //{
            using (myConnection = new OleDbConnection(myConnectionString))
            {
                myConnection.Open();
                try
                {
                    //string mySqlString = "UPDATE Shippers SET CompanyName = @CompanyName, Phone = @Phone WHERE ShipperID = @ID";
                    string mySqlString = "INSERT INTO Svirke (Naslov, Datum, Lokacija, [Kontakt osoba], [Kontakt broj], Pogodba, Zarada, Komentar) VALUES (@Naslov, @Datum, @Lokacija, @Kontaktosoba, @Kontaktbroj, @Pogodba, @Zarada, @Komentar)" ;
                  //SqlCommand MyCommand = new SqlCommand("INSERT INTO Products (Product_Name, Product_Price) Values (@ProductName, @productPrice)", myConnection);

                    OleDbCommand myCom = new OleDbCommand(mySqlString, myConnection);
                    myCom.Parameters.AddWithValue("@Naslov", textBox1.Text);
                    myCom.Parameters.AddWithValue("@Datum", dateTimePicker1.Text);
                    myCom.Parameters.AddWithValue("@Lokacija", textBox2.Text);
                    myCom.Parameters.AddWithValue("@Kontaktosoba",  textBox3.Text);
                    myCom.Parameters.AddWithValue("@Kontaktbroj",  textBox4.Text);
                    myCom.Parameters.AddWithValue("@Pogodba", textBox5.Text);
                    myCom.Parameters.AddWithValue("@Zarada", textBox6.Text);  //dataGridViewShippers.SelectedRows[0].Cells[0].Value.ToString()
                    myCom.Parameters.AddWithValue("@Komentar", richTextBox1.Text);
                    myCom.ExecuteNonQuery();
                    MessageBox.Show("Uspješan unos svirke");


                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, "Svirka neuspješno unešena", MessageBoxButtons.OK);
                }
                myConnection.Close();
            }

            BindDataGrid();
        //}
    }

那么,如何让数据保持在DataGrid和Access数据库中呢?

0 个答案:

没有答案