VB.NET - Visual Studio 2017 专业版 - Winforms
我正在创建一个 SQL 控件类,我可以在其中简单地通过键入以下内容从另一个表单运行查询:
SQL.ExecQuery("SELECT * FROM ...")
到目前为止,我所拥有的效果很好。但是,当我执行选择查询时,我只会返回记录计数。当我插入、更新或删除时,我没有得到记录计数。
我知道我必须使用ExecuteNonQuery
那些,但是我如何将它写入我已经拥有的这个方法中:
Public Sub ExecQuery(Query As String)
''Reset Query Statistics
RecordCount = 0
Exception = ""
Try
SQLCon.Open()
''Create SQL Command
SQLCmd = New SqlCommand(Query, SQLCon)
''Load parameters into SQL Command
Params.ForEach(Sub(x) SQLCmd.Parameters.Add(x))
''Clear paramater List
Params.Clear()
''Execute Command & fill dataset
SQLDS = New DataSet
SQLDA = New SqlDataAdapter(SQLCmd)
RecordCount = SQLDA.Fill(SQLDS)
SQLCon.Close()
Catch ex As Exception
''Capture error
Exception = "ExecQuery Error: " & vbNewLine & ex.Message
MessageBox.Show(Exception, "SQL QUERY FAILED!", MessageBoxButtons.OK, MessageBoxIcon.Error)
MessageBox.Show(Query)
Finally
''Close Connection
If SQLCon.State = ConnectionState.Open Then SQLCon.Close()
End Try
End Sub
有人可以给我一个示例,说明如何将其ExecuteNonQuery
合并到此子中,以便获得插入、删除或修改的记录数?