读书人

关于SQL连接有关问题

发布时间: 2013-09-28 10:01:20 作者: rapoo

关于SQL连接问题
Dim thisConnection_1 As New SqlConnection("Data Source=172.31.70.11; Initial Catalog=data; user id=user_1; password=123456;")
thisConnection_1.Open()
Dim thisConnection_2 As New SqlConnection("Data Source=172.31.70.11; Initial Catalog=data; user id=user_1; password=123456;")
thisConnection_2.Open()
Dim update_sql_1 As String = "update table1 set id = 1"
Dim update_sql_2 As String = " update table2 set id =2"
Dim cmd As New SqlCommand
cmd.Connection = thisConnection_1
cmd.CommandText = update_sql_1
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
thisConnection_1.Close()
Dim cmd_2 As New SqlCommand
cmd_2.Connection = thisConnection_2
cmd_2.CommandText = update_sql_2
Try
cmd_2.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
thisConnection_2.Close()



以上的代码.因为因为thisconnection_1 和thisconnection_2 是一样的.有办法只调用一个conn吗?
[解决办法]
可以,直接 cmd_2.Connection = thisConnection_1就可以了,thisConnection_2可以不用申请了全部删除了。当然thisConnection_1.Close()要放在使用结束后
[解决办法]

引用:
恩 这个好像可以 但是在另外一个调用sqldatareader 不行

虽然reader其本不占用资源,但的利就有弊,不过现动辄几G的内存没必要考虑了吧?

读书人网 >VB Dotnet

热点推荐