主题:初学者求救(关于VB.NET连接SQL)
想使用SQLCOMMAND控件删除数据库中某个数据,却出现
"未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。"
错误指向SqlCommand1.ExecuteNonQuery()这句
请问是什么地方不对呢?
代码如下:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents SqlDataAdapter2 As System.Data.SqlClient.SqlDataAdapter
Friend WithEvents SqlSelectCommand2 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlInsertCommand2 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlUpdateCommand2 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlDeleteCommand2 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Friend WithEvents DataSet11 As WindowsApplication16.DataSet1
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlDataAdapter2 = New System.Data.SqlClient.SqlDataAdapter
Me.SqlDeleteCommand2 = New System.Data.SqlClient.SqlCommand
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.SqlInsertCommand2 = New System.Data.SqlClient.SqlCommand
Me.SqlSelectCommand2 = New System.Data.SqlClient.SqlCommand
Me.SqlUpdateCommand2 = New System.Data.SqlClient.SqlCommand
Me.DataSet11 = New WindowsApplication16.DataSet1
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand
Me.Button1 = New System.Windows.Forms.Button
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'SqlDataAdapter2
'
Me.SqlDataAdapter2.DeleteCommand = Me.SqlDeleteCommand2
Me.SqlDataAdapter2.InsertCommand = Me.SqlInsertCommand2
Me.SqlDataAdapter2.SelectCommand = Me.SqlSelectCommand2
Me.SqlDataAdapter2.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "TABLE1", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("品名", "品名"), New System.Data.Common.DataColumnMapping("价格", "价格"), New System.Data.Common.DataColumnMapping("编号", "编号"), New System.Data.Common.DataColumnMapping("数量", "数量"), New System.Data.Common.DataColumnMapping("备注", "备注")})})
Me.SqlDataAdapter2.UpdateCommand = Me.SqlUpdateCommand2
'
'SqlDeleteCommand2
'
Me.SqlDeleteCommand2.CommandText = "DELETE FROM TABLE1 WHERE (品名 = @Original_品名) AND (价格 = @Original_价格 OR @Original_" & _
"价格 IS NULL AND 价格 IS NULL) AND (备注 = @Original_备注 OR @Original_备注 IS NULL AND 备注" & _
" IS NULL) AND (数量 = @Original_数量 OR @Original_数量 IS NULL AND 数量 IS NULL) AND (编号" & _
" = @Original_编号 OR @Original_编号 IS NULL AND 编号 IS NULL)"
Me.SqlDeleteCommand2.Connection = Me.SqlConnection1
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_品名", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "品名", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_价格", System.Data.SqlDbType.Float, 8, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "价格", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_备注", System.Data.SqlDbType.VarChar, 20, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "备注", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_数量", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "数量", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_编号", System.Data.SqlDbType.VarChar, 12, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "编号", System.Data.DataRowVersion.Original, Nothing))
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation id=""TQ08SYU-B67824A"";packet size=4096;integrated security=SSPI;data s" & _
"ource=""TQ08SYU-B67824A"";persist security info=False;initial catalog=huima"
'
'SqlInsertCommand2
'
Me.SqlInsertCommand2.CommandText = "INSERT INTO TABLE1(品名, 价格, 编号, 数量, 备注) VALUES (@品名, @价格, @编号, @数量, @备注); SELECT 品" & _
"名, 价格, 编号, 数量, 备注 FROM TABLE1 WHERE (品名 = @品名)"
Me.SqlInsertCommand2.Connection = Me.SqlConnection1
Me.SqlInsertCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@品名", System.Data.SqlDbType.VarChar, 10, "品名"))
Me.SqlInsertCommand2.Parameters.Add(New
"未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。"
错误指向SqlCommand1.ExecuteNonQuery()这句
请问是什么地方不对呢?
代码如下:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents SqlDataAdapter2 As System.Data.SqlClient.SqlDataAdapter
Friend WithEvents SqlSelectCommand2 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlInsertCommand2 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlUpdateCommand2 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlDeleteCommand2 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Friend WithEvents DataSet11 As WindowsApplication16.DataSet1
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlDataAdapter2 = New System.Data.SqlClient.SqlDataAdapter
Me.SqlDeleteCommand2 = New System.Data.SqlClient.SqlCommand
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.SqlInsertCommand2 = New System.Data.SqlClient.SqlCommand
Me.SqlSelectCommand2 = New System.Data.SqlClient.SqlCommand
Me.SqlUpdateCommand2 = New System.Data.SqlClient.SqlCommand
Me.DataSet11 = New WindowsApplication16.DataSet1
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand
Me.Button1 = New System.Windows.Forms.Button
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'SqlDataAdapter2
'
Me.SqlDataAdapter2.DeleteCommand = Me.SqlDeleteCommand2
Me.SqlDataAdapter2.InsertCommand = Me.SqlInsertCommand2
Me.SqlDataAdapter2.SelectCommand = Me.SqlSelectCommand2
Me.SqlDataAdapter2.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "TABLE1", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("品名", "品名"), New System.Data.Common.DataColumnMapping("价格", "价格"), New System.Data.Common.DataColumnMapping("编号", "编号"), New System.Data.Common.DataColumnMapping("数量", "数量"), New System.Data.Common.DataColumnMapping("备注", "备注")})})
Me.SqlDataAdapter2.UpdateCommand = Me.SqlUpdateCommand2
'
'SqlDeleteCommand2
'
Me.SqlDeleteCommand2.CommandText = "DELETE FROM TABLE1 WHERE (品名 = @Original_品名) AND (价格 = @Original_价格 OR @Original_" & _
"价格 IS NULL AND 价格 IS NULL) AND (备注 = @Original_备注 OR @Original_备注 IS NULL AND 备注" & _
" IS NULL) AND (数量 = @Original_数量 OR @Original_数量 IS NULL AND 数量 IS NULL) AND (编号" & _
" = @Original_编号 OR @Original_编号 IS NULL AND 编号 IS NULL)"
Me.SqlDeleteCommand2.Connection = Me.SqlConnection1
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_品名", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "品名", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_价格", System.Data.SqlDbType.Float, 8, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "价格", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_备注", System.Data.SqlDbType.VarChar, 20, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "备注", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_数量", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "数量", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_编号", System.Data.SqlDbType.VarChar, 12, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "编号", System.Data.DataRowVersion.Original, Nothing))
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation id=""TQ08SYU-B67824A"";packet size=4096;integrated security=SSPI;data s" & _
"ource=""TQ08SYU-B67824A"";persist security info=False;initial catalog=huima"
'
'SqlInsertCommand2
'
Me.SqlInsertCommand2.CommandText = "INSERT INTO TABLE1(品名, 价格, 编号, 数量, 备注) VALUES (@品名, @价格, @编号, @数量, @备注); SELECT 品" & _
"名, 价格, 编号, 数量, 备注 FROM TABLE1 WHERE (品名 = @品名)"
Me.SqlInsertCommand2.Connection = Me.SqlConnection1
Me.SqlInsertCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@品名", System.Data.SqlDbType.VarChar, 10, "品名"))
Me.SqlInsertCommand2.Parameters.Add(New