[英]Data not saving in database using C# and Visual Studio
I have a project that used to work fine last time I worked on it, but after I formatted the PC the program couldn't find the database, the solution I found was to use this : 我有一个上次使用它时可以正常工作的项目,但是在格式化PC后,程序找不到数据库,我发现的解决方案是使用此方法:
SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|PAPPloran.mdf;Integrated Security=True;Connect Timeout=30");
Instead having the full path for the database file location. 而是具有数据库文件位置的完整路径。
That was the change that might be causing my problem, data isn't being saved on database anymore, it is shown on a datagridview on program after I "save" but when I close and reopen the program isn't there anymore. 那是可能导致我的问题的更改,数据不再保存在数据库中,而是在我“保存”后显示在程序的datagridview上,但是当我关闭并重新打开程序后,该程序就不再存在。
Example from my program of a save button: 我的程序中有一个保存按钮的示例:
private void fusionButton1_Click(object sender, EventArgs e)
{
{
if (textBox1.Text != "" & dateTimePicker1.Text != "" & textBox3.Text != "" & textBox4.Text != "" & textBox5.Text != "" & comboBox2.Text != "" & textBox7.Text != "" & textBox8.Text != "" & textBox9.Text != "")
{
cn.Open();
cmd.CommandText = "INSERT INTO Pagamentos ( NomeFornecedor, NFatura, Data, Valor, IVA, Total, Situacao, MPagamento, Dataliquidacao, Obs) VALUES ( '" + comboBox1.Text + "', '" + textBox1.Text + "', '" + dateTimePicker1.Text + "', '" + textBox3.Text + "' , '" + textBox4.Text + "','" + textBox5.Text + "', '" + comboBox2.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "')";
cmd.ExecuteNonQuery();
cmd.Clone();
MessageBox.Show("Pagamento Inserido");
cn.Close();
}
}
}
查看bin / debug文件夹,您将在其中找到databasr文件的副本(其中包含数据)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.