简体   繁体   English

SQL插入数据无法正常工作

[英]SQL Insert Data not working properly

I want to insert some values into my table based on a users input. 我想根据用户输入将一些值插入表中。 I get these values from some textboxes. 我从一些文本框中获得这些值。 When i put a breakpoint before my SQL logic, and go through every line with F10, everything works just fine. 当我在SQL逻辑之前放置一个断点,并遍历F10的每一行时,一切正常。 I open the SQLConnection , create a SQLCommand , execute it, and close the connection again. 我打开SQLConnection ,创建一个SQLCommand ,执行它,然后再次关闭连接。 I refresh the table and have all the values in there. 我刷新表,并在其中具有所有值。 But when I delete or disable the breakpoint, and the programm goes over these lines of code by itself, it doesn't work, meaning no values are being added to the table, no matter how often i refresh. 但是,当我删除或禁用断点,并且程序本身仅遍历这些代码行时,它不起作用,这意味着无论刷新频率如何,都不会向表中添加任何值。

Here is the code I'm referring to: 这是我指的代码:

try
{
     SqlConnection con = new SqlConnection("Server=...;Database=...;Integrated Security=true;");
     con.Open();

     SqlCommand com = new SqlCommand("INSERT INTO TestTable (Type,Serialnumber) VALUES('" + TypeText + "','" + SerText + "')", con);
     //Debug.WriteLine(com.CommandText);

     com.BeginExecuteNonQuery();

     con.Close();
}
catch(Exception ex)
{
     MessageBox.Show(ex.Message);
}

When it runs on your own, it calls BeginExecuteNonQuery() and right next line connection is closed without waiting the async call to end. 当它自己运行时,它将调用BeginExecuteNonQuery()并关闭下一行连接,而无需等待异步调用结束。 While using the debugger doing F10 you give it time to efectively end the execution. 使用调试器执行F10时,您可以花时间有效地结束执行。 Consider using ExecuteNonQuery() instead, note the lack of the Begin. 考虑改用ExecuteNonQuery() ,请注意缺少Begin。

The BeginExecuteNonQuery method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that does not return rows, so that other tasks can run concurrently while the statement is executing. BeginExecuteNonQuery方法启动异步执行不返回行的Transact-SQL语句或存储过程的过程,以便在执行该语句时可以同时运行其他任务。 When the statement has completed, developers must call the EndExecuteNonQuery method to finish the operation. 语句完成后,开发人员必须调用EndExecuteNonQuery方法以完成操作。 The BeginExecuteNonQuery method returns immediately (CommandTimeout has no effect on BeginExecuteNonQuery), but until the code executes the corresponding EndExecuteNonQuery method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same SqlCommand object. BeginExecuteNonQuery方法立即返回(CommandTimeout对BeginExecuteNonQuery无效),但是直到代码执行相应的EndExecuteNonQuery方法调用之前,它不得执行对同一SqlCommand对象启动同步或异步执行的任何其他调用。 Calling the EndExecuteNonQuery before the command's execution is completed causes the SqlCommand object to block until the execution is finished. 在命令执行完成之前调用EndExecuteNonQuery会导致SqlCommand对象阻塞,直到执行完成为止。

you need to consider many things before writing any DML code, kindly consider below link which gives you the way to handling resources. 在编写任何DML代码之前,您需要考虑很多事情,请考虑以下链接,该链接为您提供了处理资源的方式。

https://stackoverflow.com/a/12142911/6783279 https://stackoverflow.com/a/12142911/6783279

Best to use using with ExecuteNonQuery than BeginExecuteNonQuery 最好与ExecuteNonQuery一起使用,而不是BeginExecuteNonQuery

 try
            {
                using (SqlConnection conn = new SqlConnection("your Connection string"))
                {
                    conn.Open();

                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                         cmd.CommandText = @"INSERT INTO TestTable 
                                                (Type,Serialnumber )
                                                VALUES (@type,@serialnumber)";


                        cmd.Parameters.AddWithValue("@type", type);
                        cmd.Parameters.AddWithValue("@serialnumber", serialnumber);

                        int rows = cmd.ExecuteNonQuery();

                        if (rows == 0)
                            throw new Exception("Nothing Inserted into the DB");
                    }   
                }
            }
            catch (Exception ex)
            {
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM