-2
哪一个合适?我需要关于ADO.net的帮助
Methot 1
Dim Connection As New SqlConnection
Dim CMD As New SqlCommand
Connection = New SqlConnection(My.Settings.ConnectionString)
CMD = Connection.CreateCommand()
CMD.Connection.Open()
CMD.CommandText = "Insert Into TableName................."
CMD.ExecuteNonQuery()
CMD.CommandText = "Update TableName Set................."
CMD.ExecuteNonQuery()
Connection.Close()
Connection.Dispose()
CMD.Dispose()
Methot 2
Dim Connection As New SqlConnection
Dim CMD As New SqlCommand
Connection = New SqlConnection(My.Settings.ConnectionString)
Connection.Open()
CMD = Connection.CreateCommand()
CMD.CommandText = "Insert Into TableName................."
CMD.ExecuteNonQuery()
CMD.CommandText = "Update TableName Set................."
CMD.ExecuteNonQuery()
Connection.Close()
Connection.Dispose()
CMD.Dispose()
两种方法都工作得很好。但我很困惑使用哪一个。请帮忙。
有什么区别? – David
好的......你为什么认为*有区别?解释你在问什么。我发现这两者之间唯一不同的是你交换了创建查询对象的行并打开连接。这两条线的顺序本身并不重要。 – David
1.使用命令打开连接 2.使用SQLConnection打开连接 –