如何在程序中检查数据库连接? [英] how to check databases connections in program?

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

问题描述

在我的项目中,用户可以拥有一种数据库,例如MS SQL Server,MY SQL,... 在表单中,用户可以选择数据库的类型及其属性(数据库名称,用户名...)
当用户单击检查按钮时,我要检查数据库连接是否正确!

In my project user can have a kind of databases ,such as MS SQL Server,MY SQL,...
In a form ,user can select the type of DataBase and its properties(databaseName,username,...)
when the user click on check button,i want to check DataBase Connections if it is true or not!

推荐答案

唯一的方法是进行试用连接:设置SqlConnection对象或MySqlConnection(或任何合适的对象),然后在try ... catch块中将其打开.如果成功,则数据库是正确且可访问的.如果不是,那就不是.

任何其他方式(例如检查服务器实例是否存在)都不会验证用户登录详细信息.
The only way to do it is to do a trial connection: set up a SqlConnection object, or a MySqlConnection (or whatever is appropriate) and Open it in a try...catch block. If it succeeds, the database is correct and reachable. If it doesn''t, it isn''t.

Any other way, such as checking to see if the server instance exists will not verify user login details.



您可以使用此
Hi ,
You can use this
SqlConnection con = new SqlConnection("your connection");
       if (con.State == System.Data.ConnectionState.Closed)
       {
           //con.open();
       }


最好的问候
米特瓦里(M.Mitwalli)


Best Regards
M.Mitwalli


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

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