OleDbCommand执行查询无效的解决

今日连接数据库,写下以下代码

   Dim  conn  As   New  OleDbConnection
        
Dim  cmd  As   New  OleDbCommand()
        conn.ConnectionString 
=  ConfigurationManager.ConnectionStrings( " AccessConn " ).ConnectionString
        cmd.Connection 
=  conn
   conn.Open()
        cmd.CommandText 
=  SQLstr       
        
Dim  previousConnectionState  As  ConnectionState  =  conn.State
        
Try
            
If  conn.State  =  ConnectionState.Closed  Then
                conn.Open()
            
End   If
            cmd.ExecuteNonQuery()
            conn.Close()
            showph(PlaceHolder3)
        
Catch  ex  As  Exception
            l_e.Text 
=   " 出现错误 "   &  ex.Message
        
Finally
            
If  previousConnectionState  =  ConnectionState.Closed  Then
                conn.Close()
            
End   If
        
End   Try

 

发现无效,于是百端调试,未见其果

最后终于于偶然发现结症所在,上面代码conn.open过早,前半部分调整为

 

   Dim  conn  As   New  OleDbConnection
        
Dim  cmd  As   New  OleDbCommand()
        conn.ConnectionString 
=  ConfigurationManager.ConnectionStrings( " AccessConn " ).ConnectionString
   conn.Open()
        cmd.CommandText 
=  SQLstr       
        
Dim  previousConnectionState  As  ConnectionState  =  conn.State
        cmd.Connection 
=  conn

即可

你可能感兴趣的:(OleDbCommand执行查询无效的解决)