c#读取,修改配置信息,并连接数据库代码测试
WinFrm窗体设置入下:
App.config文件设置如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="server" value="value"/>
<add key="database" value="db"/>
<add key="User ID" value="sa"/>
<add key="Password" value="password"/>
</appSettings>
</configuration>
点击读取按钮,获取当前配置信息到文本框中显示:
使用ConfigurationManager 需引入using System.Configuration包; 并且添加引用System.Configuration(项目----添加引用---找到System.Configuration---点南击)方可使用
/// <summary>
/// 获取配值文件信息
/// </summary>
private void GetConfig()
{
this.txtServerAdr.Text = ConfigurationManager.AppSettings["server"];
this.txtDatabase.Text = ConfigurationManager.AppSettings["database"];
this.txtUser.Text=ConfigurationManager.AppSettings["User ID"];
this.txtPassword.Text=ConfigurationManager.AppSettings["PassWord"];
}
点击修改按钮,可以前文本框中的值修改配置文件
/// <summary>
/// 根据当前填入的信息更新配置文档
/// </summary>
private void SaveConfig()
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["ServerAdr"].Value =this.txtServerAdr.Text;
configuration.AppSettings.Settings["database"].Value = this.txtDatabase.Text;
configuration.AppSettings.Settings["User ID"].Value = this.txtUser.Text;
configuration.AppSettings.Settings["PassWord"].Value = this.txtPassword.Text;
configuration.Save();
}
服务器连接测试,需要引入System.Net.Sockets包
/// <summary>
/// 服务器连接
/// </summary>
/// <param name="host">服务器IP地址</param>
/// <param name="port">端口号</param>
/// <param name="millisecondsTimeout">等待时时</param>
/// <returns></returns>
public static bool TestConnection(string host, int port, int millisecondsTimeout)
{
//需要引入System.Net.Sockets包,方可使用TcpClient对象
TcpClient client = new TcpClient();
try
{
var ar = client.BeginConnect(host, port, null, null);
ar.AsyncWaitHandle.WaitOne(millisecondsTimeout);
return client.Connected;
}
catch (Exception e)
{
throw e;
}
finally
{
client.Close();
}
}
数据库连接测试
/// <summary>
/// 数据库连接测试
/// </summary>
/// <param name="ConnectionString"></param>
/// <returns></returns>
private static bool TestConnection(string ConnectionString)
{
bool result = true;
try
{
SqlConnection conn = new SqlConnection(ConnectionString);
if (ConnectionState.Closed == conn.State)
{
conn.Open();
}
//数据库操作......
if (ConnectionState.Open==conn.State)
{
conn.Close();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
result = false;
}
return result;
}
填好文本框内容后,点击测试连接按钮
private void btnTest_Click(object sender, EventArgs e)
{
string connstr = "server=" + this.txtServerAdr.Text + ";database=" + this.txtDatabase.Text + "; User ID=" + this.txtUser.Text + ";Password=" + this.txtPassword.Text;
string[] s = connstr.Split(';');
s = s[0].Split('=');
//获取IP
string strIP =s[1];
//发送数据,判断是否连接到指定ip
if (TestConnection(strIP , 1433, 500))
{
//连接成功
MessageBox.Show("服务器连接成功","连接服务器");
// 数据库操作,我这里用了连接测试。可根据你的系统自行修改
if (TestConnection(connstr))
MessageBox.Show("数据库连接成功","连接数据库");
else
MessageBox.Show("数据库连接失败", "连接数据库");
}
else
MessageBox.Show("服务器连接失败","连接服务器");
}