Вставить в базу данных mdf не работает
Привет,
Я могу читать данные из МДФ, но не могу их сохранить. Мои коды ниже. Я делаю вещи внутри DatabaseConfig. cs и отправляю значения из SalesScreen.cs
Что я уже пробовал:
DatabaseConfig.в CS:
SqlConnection connect = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BombusDB.mdf;Integrated Security=True;"); public int buyerCheckAndSave(string buyerPhone, string buyerName, string buyerAddress, bool containsItem) { int buyerID = 0; if (containsItem) //if customer exist just find id. { connect.Open(); SqlCommand comm = new SqlCommand("SELECT TOP(1) ID FROM buyer_Tab WHERE pho LIKE @pho ORDER BY ID"); comm.Parameters.AddWithValue("@pho", '%' + buyerPhone + '%'); comm.Connection = connect; using (SqlDataReader ctr = comm.ExecuteReader()) { while (ctr.Read()) { buyerID = ctr.GetInt32(0); } } connect.Close(); return buyerID; } else //if not first save { try { connect.Open(); SqlCommand comm = new SqlCommand("INSERT INTO buyer_Tab (customer, pho, adr) VALUES ('@customer', '@pho', '@adr')"); comm.CommandType = CommandType.Text; comm.Connection = connect; comm.Parameters.AddWithValue("@customer", buyerName); comm.Parameters.AddWithValue("@pho", buyerPhone); comm.Parameters.AddWithValue("@adr", buyerAddress); comm.ExecuteNonQuery(); connect.Close(); } catch(SqlException e) { System.Windows.Forms.MessageBox.Show("Hata Olustu! \n" + e); } buyerCheckAndSave(buyerPhone, buyerName, buyerAddress, true); //after saving get id } return buyerID; }
SalesScreen.КС (приложения WinForms)
private void sellBtn_Click(object sender, EventArgs e) { billPrintDialog.Document = printBill; billPrintDialog.ShowDialog(); printBill.Print(); int customerID = 0; if (!String.IsNullOrEmpty(phoneTB.Text)) { bool containsItem = customerPhones.Any(item => item.StartsWith(phoneTB.Text)); //We have existing customers on list customerID = db.buyerCheckAndSave(phoneTB.Text, nameTB.Text, adrRTB.Text, containsItem); } MessageBox.Show(""+customerID); }