为什么这样写不对。

zymfy 2003-01-14 04:13:24
'-----------连接数据库
Dim reconn As New SqlClient.SqlConnection(strconn)
'---------建立缓冲---------------
Dim rsadapter As New SqlClient.SqlDataAdapter()
'----------建立记录集-------------------
Dim rsset As New DataSet()
'---------建立command
Dim rsczyb As New SqlClient.SqlCommand("select czybh,czyxm,mm from czyb where czybh='" & czybh & "'", reconn)
rsadapter.SelectCommand = rsczyb
rsadapter.Fill(rsset, "czyb")
'------------------------------------------
If Trim(txtjkl.Text) <> rsset.Tables("czyb").Rows(0).Item(2) Then
MsgBox("旧口令错误,请重新输入", 16, "提示")
txtjkl.Focus()
Exit Sub
End If
'--------------------------
If Trim(txtxkl.Text) <> Trim(txtqrkl.Text) Then
MsgBox("两次新密码输入不一至,请重新输入", 48, "提示信息")
txtxkl.Text = ""
txtqrkl.Text = ""
txtxkl.Focus()
Exit Sub
End If
'--------开始保存-------------
'---------建立回滚----------------
reconn.Open()
'----------------------------------------------
Dim mytrans As SqlClient.SqlTransaction = reconn.BeginTransaction()
Dim mycommand As New SqlClient.SqlCommand()
mycommand.Transaction = mytrans
Try
mycommand.CommandText = "update czyb set mm='" & Trim(txtxkl.Text) & "' where czybh='" & czybh & "'"
mycommand.ExecuteNonQuery()
mytrans.Commit()
'-----------------保存结束后--------------------
MsgBox("保存成功", 48, "提示")
Catch err As Exception
mytrans.Rollback()
MsgBox("保存失败!", 48, "提示")
MsgBox(err.ToString)
Console.WriteLine(err.ToString())
Finally
reconn.Close()
End Try
'-----------------------------------------
当运行到 mycommand.ExecuteNonQuery()时提示CONNECTION尚未初始化.
那位能告诉我怎么做。随便看一下我的代码,是不是可以简化。我是第一次用VB。NET写东东。菜的要命.help ! help !
...全文
44 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zymfy 2003-01-15
  • 打赏
  • 举报
回复
help
zymfy 2003-01-15
  • 打赏
  • 举报
回复
谢谢!
zag 2003-01-15
  • 打赏
  • 举报
回复
Dim mytrans As SqlClient.SqlTransaction = reconn.BeginTransaction()
Dim mycommand As New SqlClient.SqlCommand()
mycommand.Transaction = mytrans
Try
mycommand.CommandText = "update czyb set mm='" & Trim(txtxkl.Text) & "' where czybh='" & czybh & "'"
mycommand.ExecuteNonQuery()
改为:
Dim comtext AS String
comtext = "update czyb set mm='" & Trim(txtxkl.Text) & "' where czybh='" & czybh & "'"
Dim mytrans As SqlClient.SqlTransaction = reconn.BeginTransaction()
Dim mycommand As New SqlClient.SqlCommand(comtext ,reconn,mytrans)
Try
mycommand.ExecuteNonQuery()
可能更清晰些。
zymfy 2003-01-14
  • 打赏
  • 举报
回复
to Wolfyee(Finance)
只需加这一句就可以了。
mycommand.Connection = reconn


非常感谢。再请大家帮俺看看俺的代码是不是可以优化。或者有无用的代码 。
Wolfyee 2003-01-14
  • 打赏
  • 举报
回复
在mycommand.ExecuteNonQuery()前加两句:
mycommand.connection=reconn
reconn.close
mycommand.connection.open()
zymfy 2003-01-14
  • 打赏
  • 举报
回复
还是不行。。。55555555
zymfy 2003-01-14
  • 打赏
  • 举报
回复
ok
马上试
wyfzy521 2003-01-14
  • 打赏
  • 举报
回复
把这一句reconn.Open()放到
Dim rsadapter As New SqlClient.SqlDataAdapter()
前面试一下

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧