在c#中插入数据库 [英] insert to database in c#

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

问题描述





这是我插入数据库的代码....但它会停止并在connection.Open();中显示运行时错误CmdSql.ExecuteNonQuery();



需要你的帮助才能继续前进



Hi

this is my code for insert to database .... but it will stop and show a run time error in connection.Open();and CmdSql.ExecuteNonQuery();

NEED ur help to move forward here on

SqlConnection connection = new SqlConnection();
           connection.ConnectionString = "Data Source=ASA-PC\\SQLEXPRESS; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=10";
           connection.Open();

           {
               SqlCommand CmdSql = new SqlCommand("INSERT INTO [mat_utiAndtrans](Project, Date, Team_Leader, Vendor, WO, Rate,Code,Qty,Officer,Remark) VALUES (@Project, @Date, @Team_Leader, @Vendor, @WO, @Rate,@Code,@Qty,@Officer,@Remark)", connection);
               connection.Open();
               CmdSql.Parameters.AddWithValue("@Project", txtproject.Text);
               CmdSql.Parameters.AddWithValue("@Date", txtdte.Text);
               CmdSql.Parameters.AddWithValue("@Team_Leader", txtTeamleader.Text);
               CmdSql.Parameters.AddWithValue("@Vendor", txtvendor.Text);
               CmdSql.Parameters.AddWithValue("@WO", txtwo.Text);
               CmdSql.Parameters.AddWithValue("@Rate", txtrate.Text);
               CmdSql.ExecuteNonQuery();
               connection.Close();

推荐答案

in connection();连接未关闭。连接的当前状态是打开的。



连接中的运行时错误.Open();和CmdSql.ExecuteNonQuery();

这可能意味着什么完全是不一样了。在没有 Application.DoEvents()废话的单线程应用程序中,它要么在第一个位置崩溃,要么在最后一个位置崩溃 - 但两者都有?那些从不同线程调用两次的代码行怎么样?或者由于 Application.DoEvents()而没有显示的重复条目?
"in connection (); The connection was not closed. The connection's current state is open."
and
"run time error in connection.Open();and CmdSql.ExecuteNonQuery();"
That likely means that something totally different went wrong. In a single-threaded application without the Application.DoEvents() crap, it either crashes in the first position, or it crashes in the last position - but both? What about those lines of code being called twice from different threads? Or a duplicate entry due to an Application.DoEvents() which you did not show?


其他人已经说过,你需要添加所有参数。将来,请给我们提供ACTUAL错误消息。甚至考虑阅读它,并思考它可能意味着什么....





如果公开电话也显示错误,那么您的连接字符串已损坏。



您还应该使用块或至少在您的一次性物品上调用Dispose。
AS others have said, you need to add all the parameters. In future, give us the ACTUAL error message. Even consider reading it, and thinking about what it might mean....


If the Open call also shows an error, then your connection string is broken.

You should also use using blocks or at least call Dispose on your disposable objects.


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

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