当前位置:  开发笔记 > 编程语言 > 正文

我如何测试我是否通过vb.net连接到sql DB?

如何解决《我如何测试我是否通过vb.net连接到sqlDB?》经验,为你挑选了1个好方法。

我有以下模块,我想测试连接.如何测试连接是否有效?你能否回答一下你的答案:

Imports System.Data.SqlClient

Module Module1
    Sub Main()
        ' Create a new SqlConnectionStringBuilder and
        ' initialize it with a few name/value pairs:
        Dim builder As New SqlConnectionStringBuilder(GetConnectionString())

        ' The input connection string used the 
        ' Server key, but the new connection string uses
        ' the well-known Data Source key instead.
        Console.WriteLine(builder.ConnectionString)

        ' Pass the SqlConnectionStringBuilder an existing 
        ' connection string, and you can retrieve and
        ' modify any of the elements.
        builder.ConnectionString = _
            "server=http://sql.example.com;user id=******;" & _
            "password=***********;"
        ' Now that the connection string has been parsed,
        ' you can work with individual items.
        Console.WriteLine(builder.Password)
        builder.Password = "new@1Password"
        builder.AsynchronousProcessing = True

        ' You can refer to connection keys using strings, 
        ' as well. When you use this technique (the default
        ' Item property in Visual Basic, or the indexer in C#)
        ' you can specify any synonym for the connection string key
        ' name.
        builder("Server") = "."
        builder("Connect Timeout") = 1000

        ' The Item property is the default for the class, 
        ' and setting the Item property adds the value to the 
        ' dictionary, if necessary. 
        builder.Item("Trusted_Connection") = True
        Console.WriteLine(builder.ConnectionString)

        Console.WriteLine("Press Enter to finish.")
        Console.ReadLine()
    End Sub

    Private Function GetConnectionString() As String
        ' To avoid storing the connection string in your code,
        ' you can retrieve it from a configuration file. 
        Return "Server=(local);Integrated Security=SSPI;" & _
          "Initial Catalog=AdventureWorks"
    End Function
End Module

DOK.. 8

获得连接字符串后,可以使用打开连接Connection.Open().您可以在try-catch块中执行此操作,以捕获尝试在此处打开connection.enter代码时发生的任何错误

在关闭连接之前的任何时候Connection.Close(),您都可以使用它来检查其状态Connection.State.它返回枚举中的值,例如ConnectionState.Open.



1> DOK..:

获得连接字符串后,可以使用打开连接Connection.Open().您可以在try-catch块中执行此操作,以捕获尝试在此处打开connection.enter代码时发生的任何错误

在关闭连接之前的任何时候Connection.Close(),您都可以使用它来检查其状态Connection.State.它返回枚举中的值,例如ConnectionState.Open.

推荐阅读
无名有名我无名_593
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有