简体   繁体   English

SQL 命令 INSERT 正在运行,但数据未出现在表中

[英]SQL command INSERT is working but the data not appear in table

I am executing SQL command INSERT like this in my Visual C#.NET using MS VS 2010 Express Edition:我正在使用 MS VS 2010 Express Edition 在 Visual C#.NET 中像这样执行 SQL 命令INSERT

private void btnAdd_Click(object sender, EventArgs e)
{
     SqlConnection con = new SqlConnection(Properties.Settings.Default.loginDBConnectionString);
     con.Open();
     SqlCommand cmd = new SqlCommand("INSERT INTO tblEmp (ID, firstname, lastname, email, position) VALUES ('"+textBox1.Text+"','"+textBox2.Text+"', '"+textBox3.Text+"', '"+textBox4.Text+"', '"+comboBox1.Text+"')", con);
     cmd.ExecuteNonQuery();
     con.Close();
     MessageBox.Show("Data Added!");
}

When executing this, the MessageBox showed up which means the execution was successful.执行此操作时,出现MessageBox ,表示执行成功。 But, when I checked on the table , the data that I am trying to insert before isn't appear at all.但是,当我查看 table 时,我之前尝试插入的数据根本没有出现。

I have one database ( loginDB.mdf ) with 2 tables inside : - TblLogin - contains username and password for login purpose which executed successfully.我有一个数据库( loginDB.mdf ),里面有 2 个表: - TblLogin - 包含用于登录目的的usernamepassword ,成功执行。 - tblEmp - contains employee data, this is the one that I tried to insert data to. - tblEmp - 包含员工数据,这是我尝试向其中插入数据的数据。

What I don't understand is why the MessageBox appear when in fact none inserted into my tblEmp .我不明白的是为什么MessageBox在实际上没有插入到我的tblEmp

EDIT : ConnectionString to loginDB.mdf :编辑ConnectionStringloginDB.mdf

Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\Andreas\documents\visual studio 2010\Projects\LoginApplication\LoginApplication\loginDB.mdf";Integrated Security=True;User Instance=True

The database name is loginDB.mdf instead of logindatabase.mdf as previously written.数据库名称是loginDB.mdf而不是之前编写的logindatabase.mdf I changed it to loginDB.mdf just to test it, but still no changes appear.我将其更改为loginDB.mdf只是为了测试它,但仍然没有出现任何更改。

If your c# code executes without any exceptions, it updates the database too.如果您的 c# 代码执行时没有任何异常,它也会更新数据库。 You have probably used AttachDbFilename=|DataDirectory|\\yourDB.mdf in your ConnectionString , that means the databse that is updated is located in the subfolder BIN\\DEBUG folder of your project.您可能在ConnectionString使用了AttachDbFilename=|DataDirectory|\\yourDB.mdf ,这意味着更新的数据库位于项目的子文件夹BIN\\DEBUG文件夹中。 If you want to see the updated data just attach the database located in the bin/debug folder in ssms.如果您想查看更新的数据,只需附加位于 ssms 中bin/debug文件夹中的数据库。 for more details read this post.有关更多详细信息,请阅读这篇文章。 Also make sure your table in server explorer is not already open, if it is already open you must refresh it to show updated data.还要确保服务器资源管理器中的表尚未打开,如果它已经打开,则必须刷新它以显示更新的数据。 Please note:as mentioned in the comments you should always use parameterized queries to avoid Sql Injection .请注意:如评论中所述,您应该始终使用参数化查询来避免Sql Injection

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

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