标题:请帮我看看,不管是否正常连接,为何下面代码的变量bConnect 一直为 True ...
取消只看楼主
梦幻倩影
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:162
专家分:352
注 册:2016-8-19
结帖率:100%
已结贴  问题点数:16 回复次数:3 
请帮我看看,不管是否正常连接,为何下面代码的变量bConnect 一直为 True ?
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        With st
            .ser_name = TextBox1.Text
            .dat_name = TextBox2.Text
            .use_name = TextBox3.Text
            .pwd_id = TextBox4.Text
        End With
        constring = "Data Source=" + Trim(TextBox1.Text)
        constring &= ";Initial Catalog=" + Trim(TextBox2.Text)
        constring &= ";Persist Security Info=True;User ID=" + Trim(TextBox3.Text)
        constring &= ";Password=" + Trim(TextBox4.Text)
        ' MsgBox(constring)

        Dim objConnection As New SqlConnection(constring)
        Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter()
        Dim objDataSet As DataSet = New DataSet()
        Try
            objDataAdapter.SelectCommand = New SqlCommand
            objDataAdapter.SelectCommand.Connection = objConnection
            objDataAdapter. = "select 姓名,密码,权限 from idpass"
            objDataAdapter. = CommandType.Text
            objConnection.Open()
            objDataAdapter.Fill(objDataSet, "idpass")
        Catch ex As Exception
            MsgBox("本软件与数据库服务器无法正常连接" + vbCrLf + "请检查与服务器的连接是否正常" + vbCrLf + vbCrLf + "请检查您输入的参数是否正确?" + vbCrLf + "请修改正确后再试...... ", 64, "连接错误提示")
            bConnect = False
        Finally
            bConnect = True
        End Try

        objConnection.Close()
        objDataAdapter = Nothing
        objConnection = Nothing

        If bConnect = True Then
            ss = FreeFile()
            FileOpen(ss, wjm, OpenMode.Random, , , Len(st))
            FilePut(ss, st, 1)
            FileClose(ss)
            MsgBox("参数正确,已经保存成功!", 64, "提示")
        End If

    End Sub
2016-08-30 11:48
梦幻倩影
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:162
专家分:352
注 册:2016-8-19
得分:0 
做了个参数输入表单,如下图,本想保存之前测试一下输入的参数对不对,可是条件语句不起作用,不管对不对都会保存?

2016-08-30 11:51
梦幻倩影
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:162
专家分:352
注 册:2016-8-19
得分:0 
谢谢 不说也罢 版主,原来是我理解错Finally 的作用了

另外下午我查帮助文档发现objConnection.State 可以返回与服务器的连接状态值,因此改用了如下代码
 stcon = objConnection.State
            If stcon = "1" Then
                '  MsgBox("连接成功" + stcon)
            Else
                MsgBox("连接失败" + stcon)
            End If

2016-08-30 19:49
梦幻倩影
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:162
专家分:352
注 册:2016-8-19
得分:0 
另外,在程序代码中如果 objDataAdapter. = "select 姓名,密码,权限 from idpass"  这条查询语句写错,后面的objDataAdapter.Fill(objDataSet, "idpass")
语句也会跟着出错,请问objDataAdapter.fill 有返回执行结果的状态参数吗?我找不到

也就是说如何判断一个SQL语句是否有正确执行?

2016-08-30 19:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-468292-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.134009 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved