检查是否可以连接到数据库? [英] Check whether connection to the database is possible or not?

查看:82
本文介绍了检查是否可以连接到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的C#代码连接到sql服务器数据库.有时由于某些错误(我不知道它是什么),我无法打开conn.open()引发异常,因此我需要在打开连接之前检查是否可以连接到数据库.

I need to connect to the sql server database from my c# code. sometimes because of some error( i dont know what it is) i cannot open connection that is conn.open() throws exception so i need to check whether i can connect to the database before i am opening connection.

推荐答案

请参阅此
http://forums.asp.net/t/1169538.aspx/1 [ ^ ]
Refer this
http://forums.asp.net/t/1169538.aspx/1[^]


检查您的网络连接.

看这个视频链接

http://www.youtube.com/watch?v=lWvBt6SLjB4 [ http://www.connectionstrings.com/sql-server-2005 [
Check your network connection.

look this video link

http://www.youtube.com/watch?v=lWvBt6SLjB4[^]


and also check your connection string

http://www.connectionstrings.com/sql-server-2005[^]

Thanks

SP


SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
 connection.Open();
 if ((connection.State & ConnectionState.Open) > 0)
 {
         //enable your button which will make connection to DB and do the operation
 }
 else
 {
        // make the 2nd button disable or something you need
 }








这篇关于检查是否可以连接到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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