怎么样处理OleDbCommand执行查询无效问题?
今日连接数据库,写下以下代码
DimconnAsNewOleDbConnection
DimcmdAsNewOleDbCommand()
conn.ConnectionString=ConfigurationManager.ConnectionStrings("AccessConn").ConnectionString
cmd.Connection=conn
conn.Open()
cmd.CommandText=SQLstr
DimpreviousConnectionStateAsConnectionState=conn.State
Try
Ifconn.State=ConnectionState.ClosedThen
conn.Open()
EndIf
cmd.ExecuteNonQuery()
conn.Close()
showph(PlaceHolder3)
CatchexAsException
l_e.Text="出现错误"&ex.Message
Finally
IfpreviousConnectionState=ConnectionState.ClosedThen
conn.Close()
EndIf
EndTry
DimcmdAsNewOleDbCommand()
conn.ConnectionString=ConfigurationManager.ConnectionStrings("AccessConn").ConnectionString
cmd.Connection=conn
conn.Open()
cmd.CommandText=SQLstr
DimpreviousConnectionStateAsConnectionState=conn.State
Try
Ifconn.State=ConnectionState.ClosedThen
conn.Open()
EndIf
cmd.ExecuteNonQuery()
conn.Close()
showph(PlaceHolder3)
CatchexAsException
l_e.Text="出现错误"&ex.Message
Finally
IfpreviousConnectionState=ConnectionState.ClosedThen
conn.Close()
EndIf
EndTry
发现无效,于是百端调试,未见其果
最后终于于偶然发现结症所在,上面代码conn.open过早,前半部分调整为
DimconnAsNewOleDbConnection
DimcmdAsNewOleDbCommand()
conn.ConnectionString=ConfigurationManager.ConnectionStrings("AccessConn").ConnectionString
conn.Open()
cmd.CommandText=SQLstr
DimpreviousConnectionStateAsConnectionState=conn.State
cmd.Connection=conn
DimcmdAsNewOleDbCommand()
conn.ConnectionString=ConfigurationManager.ConnectionStrings("AccessConn").ConnectionString
conn.Open()
cmd.CommandText=SQLstr
DimpreviousConnectionStateAsConnectionState=conn.State
cmd.Connection=conn
即可
本文地址:http://www.45fan.com/dnjc/72326.html