SQL命令插入工作,但数据不会出现在表 [英] SQL command INSERT is working but the data not appear in table

查看:312
本文介绍了SQL命令插入工作,但数据不会出现在表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行SQL命令插入这样在使用MS VS我的Visual C#.NET 2010防爆preSS版:

I am executing SQL command INSERT like this in my Visual C#.NET using MS VS 2010 Express Edition:

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!");
}

在执行此,在的MessageBox 露面,这意味着执行成功。但是,当我在桌子上检查,我试图之前插入数据没有出现在所有。

When executing this, the MessageBox showed up which means the execution was successful. But, when I checked on the table , the data that I am trying to insert before isn't appear at all.

我有一个数据库( loginDB.mdf ),里面2表:
- TblLogin - 包含用户名密码用于登录的目的而成功执行。
- tblEmp - 包含员工数据,这是我试图将数据插入到一个

I have one database (loginDB.mdf) with 2 tables inside : - TblLogin - contains username and password for login purpose which executed successfully. - tblEmp - contains employee data, this is the one that I tried to insert data to.

我不明白的是,为什么的MessageBox 的时候,其实没有插入出现在我的 tblEmp

What I don't understand is why the MessageBox appear when in fact none inserted into my tblEmp.

修改的ConnectionString loginDB.mdf

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

数据库名称是 loginDB.mdf 而不是 logindatabase.mdf 为previously写的。我改成了 loginDB.mdf 只是为了测试它,但仍然没有出现变化。

The database name is loginDB.mdf instead of logindatabase.mdf as previously written. I changed it to loginDB.mdf just to test it, but still no changes appear.

推荐答案

如果你的C#code的执行没有任何异常,它更新数据库也一样,也许你正在使用 AttachDbFilename = | DataDirectory目录| \\ yourDB.mdf 的ConnectionString 意味着更新位于子文件夹中DATABSE BIN \\ DEBUG 您project.if的文件夹,你想看到更新后的数据只是附加位于斌/调试文件夹中的数据库。
有关详细信息,阅读<一href=\"http://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails/17147460#17147460\">this帖子。
另外,还要确保你的服务器资源管理器表尚未打开,如果已经打开,你必须刷新它来显示更新的数据。请注意:如在评论中提到的,你应该的总是使用参数化查询,以避免的 SQL注入

if your c# code executes without any exceptions, it updates the database too, Probably you are used AttachDbFilename=|DataDirectory|\yourDB.mdf in your ConnectionString means the databse that is updated is located in the subfolder BIN\DEBUG folder of your project.if you want to see the updated data just attach the database located in the bin/debug folder in ssms. 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命令插入工作,但数据不会出现在表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆