c#读取,修改配置信息,并连接数据库代码测试
来源:互联网 发布:spark mongodb java 编辑:程序博客网 时间:2024/05/01 05:06
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("服务器连接失败","连接服务器");
}
- c#读取,修改配置信息,并连接数据库代码测试
- 通过JDBC-ODBC连接数据库,读取数据库信息并显示
- C#测试连接access数据库步骤代码
- C#调用FFMPEG,并异步读取输出信息的代码
- C# 配置信息读取
- [150504][C#]C#连接Access数据库并读取数据
- C#连接数据库测试
- hibernate配置并测试是否可以连接数据库
- C# 读取WebConfig配置信息
- c#连接数据库代码
- XmlDocument 读取节点信息并修改
- asp.net,java 之修改IP等信息,读取IP配置信息后台代码
- C#登录信息连接数据库
- java JDBC编程——从属性文件读取信息,并创建到数据库的连接
- mysql连接数据库配置信息
- oracle连接数据库配置信息
- Jmeter连接数据库配置信息
- 根据XML文档读取连接数据库信息
- nginx部署
- 从N个数里面,随机抽取M个数(可以用作抽奖随机用户)
- 从上往下打印二叉树
- 在 Spring Boot @Configuration 注解类中使用 Mybatis 查询数据库启动报错
- android 保存logcat信息到本地文件中
- c#读取,修改配置信息,并连接数据库代码测试
- 创建与合并分支
- js城市菜select菜单联动包
- const T、const T*、T *const、const T&、const T*& 的区别
- Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketT
- C++实现向量的旋转
- 共享办公室成为共享经济发展大趋势
- 串口和文件都有输出
- python3 批量缩放图片