[英]VB.net execute SQL Server ExecuteNonQuery crash
This is VB.net executes some SQL code:这是 VB.net 执行一些 SQL 代码:
Dim dc As New SqlCommand
Dim Strsql As String = ""
dc.Connection = cnn_myself
Strsql = "SET IDENTITY_INSERT gprs_route.dbo.landmark ON;
Insert into gprs_route.dbo.landmark ( Landmark_ID , Landmark_Name , Area_ID , Class_ID , Lon , Lan ) values (4189,'test1121',1,0,121432512,24967338);
SET IDENTITY_INSERT gprs_route.dbo.landmark OFF;"
dc.CommandText = Strsql
dc.ExecuteNonQuery()
it will crash at dc.ExecuteNonQuery()
and the application will popup the error message dialog show below:它将在dc.ExecuteNonQuery()
崩溃,应用程序将弹出如下所示的错误消息对话框:
System.InvalidCastException: System.InvalidCastException:
從字串 "資料表 'gprs_route.dbo.landmark' 不具" 至型別 'Boolean' 的轉換是無效的。从串字“资料表'gprs_route.dbo.landmark'不具”至型别'Boolean'的转换是典型的。
(Converting from string table 'gprs_route.dbo.landmark' to type Boolean is useless) (从字符串表 'gprs_route.dbo.landmark' 转换为类型 Boolean 是没用的)
System.FormatException: 輸入字串格式不正確(the input string format is not correct)。 System.FormatException: 输入字串格式不正确(输入的字符串格式不正确)。
於 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)于 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
於 Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)于 Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
--- 內部例外狀況堆疊追蹤的結尾(Inner exception stacktrace end) --- --- 内部异常情况--- 追踪异常的结束(Inner stacktrace end)
於 Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)于 Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
於 ITMIS.System_Setup.Btn_Yes_Click(Object sender, EventArgs e)于 ITMIS.System_Setup.Btn_Yes_Click(Object sender, EventArgs e)
於 System.Windows.Forms.Control.OnClick(EventArgs e)于 System.Windows.Forms.Control.OnClick(EventArgs e)
於 Infragistics.Win.UltraControlBase.OnClick(EventArgs e)于 Infragistics.Win.UltraControlBase.OnClick(EventArgs e)
於 Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)于 Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)
於 Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)于 Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)
於 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)于 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
於 System.Windows.Forms.Control.WndProc(Message& m)于 System.Windows.Forms.Control.WndProc(Message&m)
於 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)于 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)
於 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)于 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)
於 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)于 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
This is the table schema:这是表架构:
CREATE TABLE [dbo].[landmark]
(
[Landmark_ID] int NOT NULL IDENTITY(3064,1) ,
[Landmark_Name] nvarchar(150) NULL ,
[Area_ID] int NULL ,
[Class_ID] int NULL ,
[Lon] int NULL ,
[Lan] int NULL
)
This is full function code below这是下面的完整 function 代码
Public Function Landmark_Insert(ByVal Data() As String) As String
Dim r As SqlDataReader
Dim c As New SqlCommand("Select max(Landmark_ID) as Landmark_ID from gprs_route.dbo.landmark", cnn_myself)
r = c.ExecuteReader
r.Read()
Data(0) = (Convert.ToInt32(r.Item("Landmark_ID").ToString) + 1).ToString()
Dim dc As New SqlCommand
Dim Strsql As String = ""
Select Case Data.Length
Case 4
Strsql += "SET IDENTITY_INSERT gprs_route.dbo.landmark ON;"
Strsql += "Insert into gprs_route.dbo.landmark ( "
Strsql += "Landmark_ID , "
Strsql += "Landmark_Name , "
Strsql += "Area_ID , "
Strsql += "Class_ID , "
Strsql += "Lon , "
Strsql += "Lan ) "
Strsql += "values (" & Data(0) & ",'" & Data(1) & "'," & Data(2) & "," & Data(3) & "," & 121432512 & "," & 24967338 & ");"
Strsql += "SET IDENTITY_INSERT gprs_route.dbo.landmark OFF;"
MessageBox.Show(Strsql)
dc.Connection = cnn_myself
dc.CommandText = Strsql
Case 6
'it won't call this....
End Select
Try
MessageBox.Show("1")
dc.ExecuteNonQuery() ' crash and show Exception error dialog
MessageBox.Show("2")
Catch ex As Exception
Return ex.Message
End Try
End Function
How to fix the crash problem?如何解决崩溃问题?
If I take away try catch and SET IDENTITY_INSERT gprs_route.dbo.landmark ON/Off. 如果我带走了,请抓住并设置ID IDITYITY_INSERT gprs_route.dbo.landmark的开/关。
It's working successfully. 工作正常。
You should use sqlcommand with type Commandtype text and with Sqlparameters for sending data in insert query by setting correct dbtype You can refer below article https://visualstudiomagazine.com/articles/2017/07/01/parameterized-queries.aspx您应该使用带有 Commandtype text 类型的 sqlcommand 和 Sqlparameters 通过设置正确的 dbtype 在插入查询中发送数据 您可以参考下面的文章https://visualstudiomagazine.com/articles/2017/07/01/parameterized-queries.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.