单击按钮上的连接SQL Server VB.net [英] Connect SQL Server on button click VB.net

查看:83
本文介绍了单击按钮上的连接SQL Server VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单击按钮时连接SQL Server?

我想在文本框中手动输入服务器,用户名和密码。


How to connect SQL Server on button click?
I want to manually enter the Server, Username and Password into textboxes.

我在这里有一个代码,但不确定是否正确。

I have a code here but I'm not sure if it's correct.

Private Sub SQLConnect_Click(sender As Object, e As EventArgs) Handles SQLConnect.Click
    Dim con As SqlConnection = New SqlConnection("Server=" & SQLServer.Text & ";Database=GameDB;User Id=" & SQLUser.Text & ";Password=" & SQLPwd.Text & ";")
    con.Open()
        MsgBox("Successfully Connected!")
    'con.Close()
End Sub

Idk如何检查连接是否正确。如果输入是对还是错。

请给我完整的代码示例。谢谢!

Idk how to check if the connection is correct or not. If the input is right or wrong.
Please give me full code example. Thank you!

推荐答案

Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim connetionString As String
    Dim cnn As SqlConnection
    connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
    cnn = New SqlConnection(connetionString)
    Try
        cnn.Open()
        MsgBox("Connection Open ! ")
        cnn.Close()
    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try
End Sub
End Class

这篇关于单击按钮上的连接SQL Server VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆