数据未插入数据库,windows,c# [英] Data is not inserted into database, windows,c#

查看:62
本文介绍了数据未插入数据库,windows,c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我目前正在使用Windows应用程序项目。在那里我试图将数据插入表中。所有代码行都按预期工作,但数据没有使用代码插入表中。我正在使用SQL查询。我可以手动输入数据。我可以更新并从数据库中选择数据。但插入是不可能的。你能不能帮我找出原因......?



这是我的代码:



Hi,
I am currently working with a windows application project. In that I tried to insert data into a table. All the lines of codes are working as expected, but the data is not inserted into the table using code.I am using sql queries. I can enter data manually. I can update and select data from database. But insert is not possible. Could you please help me to find out the reason...?

here is my code:

SqlConnection con = new SqlConnection();
           con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Bill.mdf;Integrated Security=True;User Instance=True";
           SqlCommand cmd = new SqlCommand("insert into tblCustomer values('"+txtName.Text+"','"+txtPhone.Text+"','"+txtMobile.Text+"','"+txtEmail.Text+"','"+sales+"')");
           cmd.Connection = con;
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close();







我没有收到任何错误消息或异常。但是没有插入数据。





谢谢....




I didn't get any error message or exception. But the data is not inserted.


Thank you....

推荐答案

如果您没有收到错误,很可能会插入。我看不到你的代码,但我希望你做了一个try / catch来确保没有错误。



如果没有错误,那么下一步就是确保你真的需要一个用户实例。用户实例不会更新master数据库。它实际上被复制到数据库的本地实例。



要检查它是否正确插入,您可以使用数据加载器连接到数据库,执行插入并恢复结果。这应该确认insert语句已经完成了它应该做的事情。



同样,用户实例中的insert语句只适用于用户实例而不是主数据库。



欲了解更多信息:连接到用户实例 [ ^ ]
Most likely you are inserting if you are not receiving an error. I cannot see your code but I would hope you have done a try/catch to make sure there are no errors.

If there are no errors then the next step is to make sure you really need a user instance. A user instance will not update the master database. It is actually copied to a "local" instance of the database.

To check to see if it is inserting properly you can use a datareader to connect to the database, do the insert and bring back the results. This should confirm the insert statement has done what it is supposed to.

Again, the insert statement in a User Instance will only apply to the user instance NOT the main database.

For more information: Connecting to User Instances[^]


我们需要很多机会准确回答你的问题;但你应该检查这些建议:



1.检查你的ConnectionString

2.检查你的CommandText

3检查你的参数



最好的建议是使用断点跟随你的代码,看看他们是否做你想做的事。



您需要更多帮助,请告诉我们有关您项目的更多信息。
We need a lot of chance to answer your question exactly; but you should check these suggestions:

1. check your ConnectionString
2. check your CommandText
3. check your Parameters

Best suggestion is following your code by using break points and see they are doing what you want or not.

You want more help, so tell us more about your project.


SqlConnection con = new SqlConnection();

con.ConnectionString =Data Source = .\\SQLEXPRESS; AttachDbFilename = | DataDirectory | \\Bill.mdf; Integrated Security = True; User Instance = True;

SqlCommand cmd = new SqlCommand( 插入到tblCustomer(添加列名,如字段名称,电话,数据库列名称中的sequnce)值('+ txtName.Text +','+ txtPhone.Text +','+ txtMobile.Text + ','+ txtEmail.Text +','+ sales +'));

cmd.Connection = con;

con.Open ();

cmd.ExecuteNonQuery();

con.Close();
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Bill.mdf;Integrated Security=True;User Instance=True";
SqlCommand cmd = new SqlCommand("insert into tblCustomer( add column name with sequnce like fieldname name,phone,which is in database column name) values('"+txtName.Text+"','"+txtPhone.Text+"','"+txtMobile.Text+"','"+txtEmail.Text+"','"+sales+"')");
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();


这篇关于数据未插入数据库,windows,c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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