连接_配置文件
Posted stanzhou47
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了连接_配置文件相关的知识,希望对你有一定的参考价值。
public frm_Connection()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen; //本窗体启动位置设为屏幕中央;
if (ConfigurationManager.ConnectionStrings["Sql"] != null) //若配置管理器从配置文件读取到指定的连接字符串;
{
SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder(); //声明并实例化SQL连接字符串构造器;
sqlConnectionStringBuilder.ConnectionString = ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //借助配置管理器从配置文件读取完整的连接字符串,并赋予SQL连接字符串构造器的相应属性;
this.txb_Server.Text = sqlConnectionStringBuilder.DataSource; //从SQL连接字符串构造器各属性中,获取连接字符串的各个元素,并显示于相应控件上;
this.txb_Database.Text = sqlConnectionStringBuilder.InitialCatalog;
this.ckb_IsWindowsAuthentication.Checked = sqlConnectionStringBuilder.IntegratedSecurity;
}
}
/// <summary>
/// 私有方法:单击连接按钮;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Connect_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //配置管理器从配置文件读取连接字符串,并将之赋予SQL连接的连接字符串属性;
sqlConnection.Open(); //打开SQL连接;
MessageBox.Show //在消息框中显示;
("连接状态:" + sqlConnection.State.ToString() //消息框消息内容;
+ "
工作站标识:" + sqlConnection.WorkstationId
+ "
服务器地址:" + sqlConnection.DataSource
+ "
服务器版本:" + sqlConnection.ServerVersion
+ "
数据库名称:" + sqlConnection.Database
+ "
(单击【确定】后将关闭SQL连接)");
sqlConnection.Close(); //关闭SQL连接;
}
}
}
以上是关于连接_配置文件的主要内容,如果未能解决你的问题,请参考以下文章
dljd_(011-020)_hibernate中配置文件详解