我的form1.cs这是样子的: 
数据地址:192.168.61 
数据库: test 
用户名: sa 
密码:  123 测试(按钮) 问题是:我点按钮时,弹出对话框!(成功或失败) 请用C#怎么么写这段代码?我有四个输入的textbox控件,分别输入ip,数据库名,用户名,密码 
一个测试的按钮,点击时测试是否连接成功!           string strConnection = "server=" + textBox1.Text.ToString().Trim() + ";database=" + textBox2.Text.ToString().Trim() + ";uid=" + textBox3.Text.ToString().Trim() + ";pwd=" + textBox4.Text.ToString().Trim() + ";"; 
            try 
            { 
                conn.ConnectionString = strConnection; 
                conn.Open(); 
                string s=conn.State.ToString(); 
                textBox5.Text = s; 
            } 
            catch (Exception ex) 
            { 
                textBox5.Text = "失败了!"; 
            } 
            finally 
            { 
                
                conn.Close(); 
                
            } 
我这样子写应该没问题?
再跟帖问一个问题: 
我开始时不选择数据库,而给出服务器名,登录用户名(sa),密码,我使用一个comboBox的下个下拉框动态选择数据库, 
请问这个代码怎么实现?

解决方案 »

  1.   

    C#没写给你一段VB的    Dim I As Integer
        Dim sqlApp As New SQLDMO.ApplicationClass
        Dim ServerName As SQLDMO.NameList
        Dim oServer As New SQLDMO.SQLServer
        Dim oDataBase As SQLDMO.Database    Private Sub FrmSetCon_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '得到SQL服务器的列表
            '必须安装SQL SERVER 2000 SP2 及以上版本        ServerName = sqlApp.ListAvailableSQLServers
            For I = 1 To ServerName.Count
                CboServer.Items.Add(ServerName.Item(I))
            Next
        End Sub    Private Sub CboServer_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboServer.SelectedValueChanged
            'Try
            '    If Me.CboServer.Text <> "" Then
            '        oServer.Connect(Me.CboServer.Text, "sa", "")
            '    End If
            'Catch ex As Exception        'End Try
        End Sub    Private Sub BtnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTest.Click
            Try
                If Me.CboServer.Text <> "" Then
                    oServer.Connect(Me.CboServer.Text, Me.TxtUID.Text, Me.TxtPwd.Text)
                    Select Case oServer.Status
                        Case 1
                        Case 2
                        Case 3
                        Case 4
                        Case 5
                        Case 6
                        Case 7
                    End Select
                    Me.CboDatabase.Items.Clear()
                    For Each oDataBase In oServer.Databases
                        Me.CboDatabase.Items.Add(oDataBase.Name)
                    Next
                    MsgBox("测试成功!", MsgBoxStyle.Information, "提示")
                End If
            Catch ex As Exception
                Select Case Err.Number
                    Case -2147201024
                        MsgBox("数据库已经连接成功!")
                    Case -2147213048
                        MsgBox("测试失败,请检查用户名,密码是否正确", MsgBoxStyle.Information, "测试失败")
                    Case Else
                        MsgBox("测试失败,请检查用户名,密码是否正确", MsgBoxStyle.Information, "测试失败")
                End Select
            End Try
        End Sub    Private Sub BtnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLogin.Click
            If Me.CboServer.Text <> "" And Me.TxtUID.Text <> "" And Me.CboDatabase.Text <> "" Then
                If WriteReg("rServer", Me.CboServer.Text) = False Then MsgBox("初始化数据连接失败!请检查.", MsgBoxStyle.Information, "提示") : Exit Sub
                If WriteReg("rServerUID", Me.TxtUID.Text) = False Then MsgBox("初始化数据连接失败!请检查.", MsgBoxStyle.Information, "提示") : Exit Sub
                If WriteReg("rServerPWD", Me.TxtPwd.Text) = False Then MsgBox("初始化数据连接失败!请检查.", MsgBoxStyle.Information, "提示") : Exit Sub
                If WriteReg("rDatab", Me.CboDatabase.Text) = False Then MsgBox("初始化数据连接失败!请检查.", MsgBoxStyle.Information, "提示") : Exit Sub
                Me.Close()
            End If
        End Sub    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
            If ServerN = "" Then
                Application.Exit()
            Else
                Me.Close()
            End If
        End Sub
    End Class