我一直在努力弄清楚为什么我的插入没有出现在我的数据库表中。我已经完成了很多次,我没有看到错误,也没有异常(exception)。我是使用数据库的新手,所以也许我能看到一些东西,我们将不胜感激。谢谢
using(SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[EmployeeInfo]
(Id, name, password) VALUES (@Id, @name, @password)", connection)) {
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", Id++);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@password", password);
try {
connection.Open();
//rows effected
int i = cmd.ExecuteNonQuery();
MessageBox.Show("User saved");
} catch (SqlException ex) {
MessageBox.Show(ex.Message, ex.GetType().ToString());
} finally {
nameBox.Clear();
passwordBox.Clear();
connection.Close();
}
}
这是来自配置文件的连接字符串
<connectionStrings>
<add name="SBPA.Properties.Settings.EmployeeLoginConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\EmployeeLogin.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
最佳答案
Hay 伙计们,感谢大家的回复,几个小时后我终于发现我的代码没有任何问题。正在更新的数据库是 bin/debug 文件夹中的数据库,而不是我在解决方案中引用的数据库。我没有意识到在处理项目时所做的所有更改都是针对调试文件夹中的副本完成的。
关于C# SQL INSERT 未出现在数据库表 "no errors or exceptions thrown"中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27141285/