简体   繁体   English

如何在数据库中保存数据?

[英]How do I save data in my database?

I have made a datagridview with data from database, then I have 3 different forms with insert data, delete data, etc. 我用数据库中的数据做了一个datagridview,然后我有3种不同的表单,包括插入数据,删除数据等。

The problem is that when I'm trying to insert data and restart the program, the data doesn't get saved. 问题是,当我尝试插入数据并重新启动程序时,数据不会被保存。

Here is my code: 这是我的代码:

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");

con.Open();

string sql = "INSERT INTO Elevi (Nume, Prenume, SportulPracticat) VALUES(@nume, @prenume, @sport)";

using (SqlCommand cmd = new SqlCommand(sql, con))
{
    cmd.Parameters.AddWithValue("@nume", textBox1.Text);
    cmd.Parameters.AddWithValue("@prenume", textBox2.Text);
    cmd.Parameters.AddWithValue("@sport", textBox3.Text);

    cmd.ExecuteNonQuery();
}

con.Close();

The whole User Instance and AttachDbFileName= approach is flawed - at best! 整个User Instance和AttachDbFileName =方法存在缺陷 - 充其量! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\\bin\\debug - where you app runs) and most likely , your INSERT works just fine - but you're just looking at the wrong .mdf file in the end! 在Visual Studio中运行应用程序时,它将在.mdf文件周围复制(从App_Data目录复制到输出目录-通常是.\\bin\\debug应用程序运行所在的位置),并且很可能 INSERT可以正常工作-但最后您只是看错了.mdf文件

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there. 如果你想坚持这种方法,那么尝试在myConnection.Close()调用上设置一个断点 - 然后用SQL Server Mgmt Studio Express检查.mdf文件 - 我几乎可以肯定你的数据在那里。

The real solution in my opinion would be to 我认为真正的解决方案

  1. install SQL Server Express (and you've already done that anyway) 安装SQL Server Express(并且您已经完成了此操作)

  2. install SQL Server Management Studio Express 安装SQL Server Management Studio Express

  3. create your database in SSMS Express , give it a logical name (eg YourDatabase ) SSMS Express中创建数据库,给它一个逻辑名称(例如YourDatabase

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. 使用其逻辑数据库名称 (在服务器上创建时提供)连接到该数据库 -并且不要弄乱物理数据库文件和用户实例。 In that case, your connection string would be something like: 在这种情况下,您的连接字符串将类似于:

     Data Source=.\\\\SQLEXPRESS;Database=YourDatabase;Integrated Security=True 

    and everything else is exactly the same as before... 和其他一切是完全一样的前...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info. 另请参阅亚伦·贝特朗(Aaron Bertrand)出色的博客文章不良习惯:使用AttachDbFileName获取更多背景信息。

I can't spot any error. 我无法发现任何错误。 Maybe you're looking on the wrong piece of code. 也许您正在寻找错误的代码。 If idata vanish from db when starting the debug perhaps you should look to what happens before or after (do you perform a deletion or a truncate?). 如果idata在启动调试时从db中消失,那么你应该看看之前或之后会发生什么(你执行删除还是截断?)。

Try adding a COMMIT; 尝试添加COMMIT; command to the SQL code 命令到SQL代码

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
SqlTransaction trans = con.BeginTransaction();
con.Open();
string sql = "INSERT INTO Elevi (Nume , Prenume , SportulPracticat) VALUES(@nume,@prenume,@sport)";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
    cmd.Transaction = trans;
    cmd.Parameters.AddWithValue("@nume", textBox1.Text);
    cmd.Parameters.AddWithValue("@prenume", textBox2.Text);
    cmd.Parameters.AddWithValue("@sport", textBox3.Text);
    cmd.ExecuteNonQuery();
    trans.Commit();
}
con.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM