OleDbCommand执行查询无效的解决[通俗易懂]

2022-08-26 15:41:52 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

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

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

即可

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/144295.html原文链接:https://javaforall.cn

0 人点赞