我一直在随机地失败,无法将记录写入访问数据库以保持时间。它在99%的时间内都有效,但我找不到导致失败的任何模式。有一个更好的方法吗?如果是网络问题,我能否让它继续尝试使用DO while循环进行写操作,直到sql语句找到刚写的行?
我实现了一个错误日志文件,但是当它写入失败时,它在错误日志中什么都没有显示
Try
Select Case ComboBox2.Text
Case "002"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[002]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "006"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[006]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "007"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[007]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "008"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[008]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "009"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[009]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "011"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[011]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "012"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[012]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "015"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[015]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "017"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[017]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "020"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[020]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "022"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[022]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
Case "027"
sqlQry1.CommandText = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],[027]) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
End Select
sqlQry1.Parameters.AddWithValue("@D", Job_Date)
sqlQry1.Parameters.AddWithValue("@Name", ComboBox1.Text)
sqlQry1.Parameters.AddWithValue("@EWC", ComboBox3.Text)
sqlQry1.Parameters.AddWithValue("@Q", TextBox3.Text)
sqlQry1.Parameters.AddWithValue("@job_num", TextBox4.Text)
sqlQry1.Parameters.AddWithValue("@print_num", TextBox5.Text)
sqlQry1.Parameters.AddWithValue("@Prim", L_Hours)
sqlQry1.Parameters.AddWithValue("@WCC", TextBox6.Text)
sqlQry1.Parameters.AddWithValue("@t_in", Auto_In)
sqlQry1.Parameters.AddWithValue("@t_out", Job_Time)
sqlQry1.Parameters.AddWithValue("@OP_Code", L_Hours)
sqlQry1.ExecuteNonQuery()
con1.Close()
Catch ex As Exception
MessageBox.Show("Error code L07 -- Failed To Insert New Record During Job In")
Using sw As StreamWriter = File.AppendText(Error_Path)
sw.WriteLine(" ")
sw.WriteLine(DateTime.Now + " Error code L07 " + ComboBox1.Text + " " + Environment.MachineName + " " + ex.Message)
If TextBox7.Text > "" Then
sw.WriteLine("Inputs: Qty " + TextBox3.Text + " Job # " + TextBox4.Text + " Print # " + TextBox5.Text + " WC " + TextBox6.Text + " Code " + ComboBox3.Text + " Time In " + TextBox7.Text + "Time Out " + Job_Time)
sw.WriteLine("SQL Statement = " + sqlQry1.CommandText)
Else
sw.WriteLine("Inputs: Qty " + TextBox3.Text + " Job # " + TextBox4.Text + " Print # " + TextBox5.Text + " WC " + TextBox6.Text + " Code " + ComboBox3.Text)
sw.WriteLine("SQL Statement = " + sqlQry1.CommandText)
sw.WriteLine(" ")
End If
End Using
End Try
con1.Dispose()
它不会给我任何错误,当检查数据时,记录不存在。
答案 0 :(得分:0)
这里有很多可能性,因此为了缩小范围,您应该以减少发生问题的机会的方式声明变量。例如,从您的代码片段中,我无法确定您在何处或如何声明SqlConnection
和SqlCommand
对象。我看到您在异常处理程序中遇到过Using语句。您还应该对任何Using
对象使用IDisposable
。另外,您可以极大地简化该Select Case
代码块。请考虑以下因素:
Imports System.Data.SqlClient
Public Class Form1
' This would naturally be located in a centralized place in your code base,
' and is only in this Form class for example purposes:
Private Const CONN_STRING As String = "your connection string"
Private Sub Foo()
Dim myQuery As String = "INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out],{0}) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code)"
myQuery = String.Format(myQuery, ComboBox2.Text)
Using conn As New SqlConnection(CONN_STRING)
Using cmd As New SqlCommand(myQuery)
With cmd
Try
.CommandType = CommandType.Text
.Parameters.AddWithValue("@D", Job_Date)
.Parameters.AddWithValue("@Name", ComboBox2.Text)
.Parameters.AddWithValue("@EWC", ComboBox3.Text)
.Parameters.AddWithValue("@Q", TextBox3.Text)
.Parameters.AddWithValue("@job_num", TextBox4.Text)
.Parameters.AddWithValue("@print_num", TextBox5.Text)
.Parameters.AddWithValue("@Prim", L_Hours)
.Parameters.AddWithValue("@WCC", TextBox6.Text)
.Parameters.AddWithValue("@t_in", Auto_In)
.Parameters.AddWithValue("@t_out", Job_Time)
.Parameters.AddWithValue("@OP_Code", L_Hours)
.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error code L07 -- Failed To Insert New Record During Job In")
Using sw As StreamWriter = File.AppendText(Error_Path)
sw.WriteLine(" ")
sw.WriteLine(DateTime.Now + " Error code L07 " + ComboBox2.Text + " " + Environment.MachineName + " " + ex.Message)
If TextBox7.Text > "" Then
sw.WriteLine("Inputs: Qty " + TextBox3.Text + " Job # " + TextBox4.Text + " Print # " + TextBox5.Text + " WC " + TextBox6.Text + " Code " + ComboBox3.Text + " Time In " + TextBox7.Text + "Time Out " + Job_Time)
sw.WriteLine("SQL Statement = " + .CommandText)
Else
sw.WriteLine("Inputs: Qty " + TextBox3.Text + " Job # " + TextBox4.Text + " Print # " + TextBox5.Text + " WC " + TextBox6.Text + " Code " + ComboBox3.Text)
sw.WriteLine("SQL Statement = " + .CommandText)
sw.WriteLine(" ")
End If
End Using
End Try
End With
End Using
End Using
End Sub
End Class
ComboBox2
的值而不同。Using
语句用于声明SqlConnection
和SqlCommand
为什么没有写入某些行有很多可能性,但是仅基于您的代码片段,这是一个猜测。也许SqlCommand没有得到正确处理,并且正在被重用?我建议像这里一样清理代码,然后查看结果。
免责声明:这是未经测试的代码。
答案 1 :(得分:0)
我认为您的间歇性问题可能与.AddWithValue
有关。参见http://www.dbdelta.com/addwithvalue-is-evil/
和
https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
还有一个:
https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications
我使用了.Add
方法作为参数。我不得不猜测数据类型。您将必须检查数据库中的实际OleDbType。然后,确保您的.net类型的参数值匹配。在将它们分配给.Value
之前,应先对其进行验证。如果您确定转换将成功,则可能只需要CInt或CDate。
我还使用了以$开头的内插字符串。它们更容易阅读。变量仅放在大括号{}包围的行中。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim sqlQuery = $"INSERT INTO [Job Times] ([Date],[Employee], [EmpWC],[Qnty],[Job #],[Print #],[Primary], [WC],[Time In], [Time Out], [{ComboBox2.Text}] ) VALUES (@D, @Name,@EWC,@Q,@job_num,@print_num,@Prim,@WCC ,@t_in,@t_out,@OP_Code);"
Using cn As New OleDbConnection("Your connection String")
Using cmd As New OleDbCommand(sqlQuery, cn)
With cmd.Parameters
.Add("@D", OleDbType.Date).Value = Job_Date
.Add("@Name", OleDbType.VarChar).Value = ComboBox1.Text
.Add("@EWC", OleDbType.VarChar).Value = ComboBox3.Text
.Add("@Q", OleDbType.VarChar).Value = TextBox3.Text
.Add("@job_num", OleDbType.VarChar).Value = TextBox4.Text
.Add("@print_num", OleDbType.VarChar).Value = TextBox5.Text
.Add("@Prim", OleDbType.Numeric).Value = L_Hours
.Add("@WCC", OleDbType.VarChar).Value = TextBox6.Text
.Add("@t_in", OleDbType.VarChar).Value = Auto_In
.Add("@t_out", OleDbType.Numeric).Value = Job_Time
.Add("@OP_Code", OleDbType.Numeric).Value = L_Hours
End With
Try
cn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error code L07 -- Failed To Insert New Record During Job In")
Using sw As StreamWriter = File.AppendText(Error_Path)
sw.WriteLine(" ")
sw.WriteLine($"{DateTime.Now} Error code L07 {ComboBox1.Text} { Environment.MachineName} {ex.Message}")
Dim nextLine = $"Inputs: Qty {TextBox3.Text} Job # {TextBox4.Text } Print # {TextBox5.Text} WC {TextBox6.Text} Code {ComboBox3.Text}"
If TextBox7.Text <> "" Then
nextLine &= $" Time In {TextBox7.Text} Time Out {Job_Time}"
End If
sw.WriteLine(nextLine)
sw.WriteLine($"SQL Statement = {cmd.CommandText}")
sw.WriteLine(" ")
End Using
End Try
End Using
End Using
End Sub