本文介绍了“连接未关闭时该怎么办?连接的当前状态是打开的。出现在“dcnOil.Close”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 公开 类 clsOilDA 私有 共享 dcnOil 作为 新 _ OleDbConnection( Provider = Microsoft.Jet.OLEDB.4.0;数据Source = C:\ temp \Oil.mdb) 私有 共享油作为 新 ArrayList() 私有 共享 anOil 作为 clsOil 私有 共享 aCountry,aCompany 作为 字符串 私人 共享 aFax,anOrder 作为 整数 私人 共享 aPrice 作为 十进制 公共 共享 Sub Initialize() dcnOil.Close() 结束 Sub 公开 共享 Sub 终止() dcnOil.Close() dcnOil.Dispose() 结束 Sub Public 共享 函数 GetAll()作为 ArrayList Dim dapOil As New OleDbDataAdapter() Dim dtbOil 作为 新 DataTable() Dim drwOil As DataRow dapOil = 新 OleDbDataAdapter( 选择*来自油,dcnOil) dapOil.Fill(dtbOil) oil.Clear() 对于 每个 drwOil 在 dtbOil.Rows aCountry = drwOil( Country ) aCompany = drwOil( 公司) aFax = drwOil( 传真) anOrder = drwOil( Order) aPrice =( Price ) 下一步 返回石油 结束 功能 公共 共享 Sub 添加( ByVal aOil As clsOil) Dim dapOil As 新 OleDbDataAdapter() Dim sqlQuery 作为 字符串 = INSERT INTO Oil& VALUES('& aOil.Country& ','& aOil.Company& ','& aOil.Fax& ','& aOil.Order& ','& aOil.Price& ') dapOil.UpdateCommand = 新 OleDbCommand(sqlQuery,dcnOil) dapOil.UpdateCommand.ExecuteNonQuery() 结束 Sub 公开 共享 Sub 删除( ByVal aOil As clsOil) Dim dapOil As 新 OleDbDataAdapter() Dim sqlQuery As 字符串 = DELETE FROM Oil WHERE Country = '& aOil.Country& ' dapOil.UpdateCommand = 新 OleDbCommand(sqlQuery,dcnOil) dapOil.UpdateCommand.ExecuteNonQuery() 结束 Sub 结束 类 解决方案 在尝试关闭之前检查它是否实际打开。 另外 - 你似乎没有在这个类的任何地方打开连接。 尝试类似 公共 共享 Sub Initialize() 如果 dcnOil.State = ConnectionState.Open 然后 dcnOil.Close() 结束 如果 dcnOil.Open() 结束 Sub 公共 共享 Sub Terminate() 如果 dcnOil.State = ConnectionState.Open 那么 dcnOil.Close() 结束 如果 dcnOil.Dispose() 结束 子 Public Class clsOilDA Private Shared dcnOil As New _ OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\Oil.mdb") Private Shared oil As New ArrayList() Private Shared anOil As clsOil Private Shared aCountry, aCompany As String Private Shared aFax, anOrder As Integer Private Shared aPrice As Decimal Public Shared Sub Initialize() dcnOil.Close() End Sub Public Shared Sub Terminate() dcnOil.Close() dcnOil.Dispose() End Sub Public Shared Function GetAll() As ArrayList Dim dapOil As New OleDbDataAdapter() Dim dtbOil As New DataTable() Dim drwOil As DataRow dapOil = New OleDbDataAdapter("Select * from Oil", dcnOil) dapOil.Fill(dtbOil) oil.Clear() For Each drwOil In dtbOil.Rows aCountry = drwOil("Country") aCompany = drwOil("Company") aFax = drwOil("Fax") anOrder = drwOil("Order") aPrice = ("Price") Next Return oil End Function Public Shared Sub Add(ByVal aOil As clsOil) Dim dapOil As New OleDbDataAdapter() Dim sqlQuery As String = "INSERT INTO Oil " & "VALUES ('" & aOil.Country & "', '" & aOil.Company & "', '" & aOil.Fax & "', '" & aOil.Order & "', '" & aOil.Price & "') " dapOil.UpdateCommand = New OleDbCommand(sqlQuery, dcnOil) dapOil.UpdateCommand.ExecuteNonQuery() End Sub Public Shared Sub Delete(ByVal aOil As clsOil) Dim dapOil As New OleDbDataAdapter() Dim sqlQuery As String = "DELETE FROM Oil WHERE Country = '" & aOil.Country & "'" dapOil.UpdateCommand = New OleDbCommand(sqlQuery, dcnOil) dapOil.UpdateCommand.ExecuteNonQuery() End SubEnd Class 解决方案 Check to see if it is actually open before attempting to close it.Also - you don't appear to be opening the connection anywhere in this class.Try something likePublic Shared Sub Initialize() If dcnOil.State = ConnectionState.Open Then dcnOil.Close() End If dcnOil.Open()End SubPublic Shared Sub Terminate() If dcnOil.State = ConnectionState.Open Then dcnOil.Close() End If dcnOil.Dispose()End Sub 这篇关于“连接未关闭时该怎么办?连接的当前状态是打开的。出现在“dcnOil.Close”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-26 07:53