无法将表单数据插入SQL数据库 [英] Unable to insert form data to SQL database
问题描述
我有简单的表格,可以读取用户名,电子邮件和城市。我创建了一个数据库和一个包含字段ID,名称,电子邮件和城市的表。当我运行代码时,我不会遇到任何错误或异常。但是无法将插入的数据看到表格中。
以下是我的代码
I have simple form which reads users name,email and city. I have created a database and a table with fields id,name,email and city. When i run the code i don't encounter any error or exceptions. But unable to see the inserted data into table.
following is my code
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=E:\\Database3.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand cmd = new SqlCommand(@"insert into tbl_insert values(@Id,@name,@city)", con);
cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(txtId.Text));
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@city", txtCity.Text);
我尝试了什么:
i在一个目录中有数据库文件(.mdf)和日志文件(.ldf),在Visual Studio工作区中有项目的其余部分。通过调试我很确定数据是插入到表中但我无法在表中看到数据。
What I have tried:
i have database file (.mdf) and logfile (.ldf) in one directory and rest of the project in visual studio workspace. by debugging i am pretty sure that data is inserted into table but i am not able to see data in the table.
推荐答案
在所有参数后面的最后添加下面的代码已添加。
add below code at the last after all parameters have added.
cmd.ExecuteNonQuery();
我的代码中没有看到ExecuteNonQuery代码。
I did not see below ExecuteNonQuery code in your code.
<pre>command.ExecuteNonQuery();
如果表中的列数为tbl_insert超过3
然后你应该在插入查询中指定特定的列名...
if the number of columns in the table tbl_insert is more than 3
then u should specify the specific column name also in the insert query...
如果数字表tbl_insert中的列数超过3
那么你应该在插入查询中指定特定的列名...
if the number of columns in the table tbl_insert is more than 3
then u should specify the specific column name also in the insert query...
这篇关于无法将表单数据插入SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!