在Vb.net中检查数据库状态 [英] Check database status in Vb.net

查看:59
本文介绍了在Vb.net中检查数据库状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人可以帮忙?
我正在做有关system的项目.我正在使用Visual Basic .net 2005和sql 2005软件.在此系统中,我必须创建包含按钮的界面,该按钮用于检查数据库连接是联机还是脱机.例如,如果我的数据库未连接,它将显示消息数据库为脱机状态;如果我的数据库为连接,它将显示消息数据库已在线,并且它将定向到另一个接口..我的问题是,一旦我单击按钮,它就不会显示任何内容.下面是我的编码..需要您的建议

Hi, is there anyone could help??
I am doing my project about system . I am using software visual basic .net 2005 and sql 2005. In this system i must create interface which contain button use to check database connection if it online or offline. Example if my database is not connect it will show message database offline and if my database if connect it will show message your database if online and it will direct to another interface ..My Probleam is once i click the button it not show anything.. below is my coding.. need your advice

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class DatabaseStatus
    
    Public Shared Sub SqlConnectionExample()

        Using con As New SqlConnection
            con.ConnectionString = "Data Source=.\sqlexpress;Database=AdventureWorks;Integrated Security=SSPI;"
            con.Open()

            If con.State = ConnectionState.Open Then
                Console.WriteLine("SqlConnection Information:")
                Console.WriteLine("  Connection State = " & con.State)
                Console.WriteLine("  Connection String = " & con.ConnectionString)
                Console.WriteLine("  Database Source = " & con.DataSource)
                Console.WriteLine("  Database = " & con.Database)
                Console.WriteLine("  Server Version = " & con.ServerVersion)
                Console.WriteLine("  Workstation Id = " & con.WorkstationId)
                Console.WriteLine("  Timeout = " & con.ConnectionTimeout)
                Console.WriteLine("  Packet Size = " & con.PacketSize)
            Else
                Console.WriteLine("SqlConenction failed to open.")
                Console.WriteLine("  Connection State = " & con.State)
            End If
            con.Close()
        End Using
    End Sub

    Public Shared Sub OleDbConnectionExample()
        Using con As New OleDbConnection
            con.ConnectionString = "Provider=SQLOLEDB;Data Source=.\sqlexpress;Initial Catalog=AdventureWorks;Integrated Security=SSPI;"
            con.Open()
            If con.State = ConnectionState.Open Then
                Console.WriteLine("OleDbConnection Information:")
                Console.WriteLine("  Connection State = " & con.State)
                Console.WriteLine("  Connection String = " & con.ConnectionString)
                Console.WriteLine("  Database Source = " & con.DataSource)
                Console.WriteLine("  Database = " & con.Database)
                Console.WriteLine("  Server Version = " & con.ServerVersion)
                Console.WriteLine("  Timeout = " & con.ConnectionTimeout)
            Else
                Console.WriteLine("OleDbConnection failed to open.")
                Console.WriteLine("  Connection State = " & con.State)
            End If
            con.Close()
        End Using
    End Sub
    Public Shared Sub Main()
        SqlConnectionExample()
        OleDbConnectionExample()
    End Sub
End Class



[已编辑始终将代码包装在"pre"标签中[/已编辑]



推荐答案

一切似乎都是正确的.我测试了控制台和Windows窗体,并输出到TextBox.检查您的连接字符串.
更新:只需添加异常处理

Everything seems correct. I tested console and Windows Forms with output to TextBox. Check your connection string.
Update: Just add exception handling

Try
        ...
        Catch SqlExceptionErr As SqlException
            ''Write the exception
            Console.WriteLine(SqlExceptionErr.Message)
        Catch InvalidOperationExceptionErr As InvalidOperationException
            ''Write the exception
            Console.WriteLine(InvalidOperationExceptionErr.Message)
    End Try


这篇关于在Vb.net中检查数据库状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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