FrmDBConfig.cs
/*----------------------------------------------------------------
// Copyright (C) Project
// 文件名:
// 文件功能描述:进行数据库配置和连接
//
//
// 创建标识:
//
// 修改标识:
// 修改描述:
----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.ProviderBase;
using System.Data.SqlClient;
namespace Kitchen.UI
{
public partial class FrmDBConfig : Form
{
public FrmDBConfig()
{
InitializeComponent();
BindDataServer();
}
public FrmDBConfig(FrmWelcome fw)
{
InitializeComponent();
BindDataServer();
fw.Close();
}
/// <summary>
/// 测试数据库连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click( object sender, EventArgs e)
{
if (txtPassWord.Text.Trim() == "" || cbServerName.Text.Trim() == "" || txtUsername.Text.Trim() == "")
{
MessageBox.Show( "输入服务器名(或者IP)、用户名和密码");
return;
}
string connStr = string.Format( "Data Source={0};User ID={1};Password={2}", cbServerName.Text.Trim(), txtUsername.Text.Trim(), txtPassWord.Text.Trim());
SqlConnection conn = new SqlConnection(connStr); // sqlcp = new SqlClientPermission()
//建立连接
try
{
//myConn = new SqlConnection(connStr);
conn.Open();
}
catch (Exception ee)
{
MessageBox.Show( "连接失败");
return;
}
MessageBox.Show( "连接成功");
}
private void button4_Click( object sender, EventArgs e)
{
this.Close();
}
private void button2_Click( object sender, EventArgs e)
{
if (txtPassWord.Text.Trim() == "" || cbServerName.Text.Trim() == "" || txtUsername.Text.Trim() == "")
{
MessageBox.Show( "输入服务器名(或者IP)、用户名和密码");
return;
}
if ((System.Configuration.ConfigurationSettings.AppSettings.GetValues( "isFirstRunSystem"))[0] == "true")
{
try
{
string str = string.Format( "data source ={0};initial catalog={1};user id={2};password={3};",
cbServerName.Text.Trim(), txtDBName.Text.Trim(), txtUsername.Text.Trim(), txtPassWord.Text);
Kitchen.Util.Config cc = new Kitchen.Util.Config();
cc.UpdateDBConfig( "ProConnectionString ", str, "System.Data.SqlClient");
MessageBox.Show( "配置成功,软件将自动重启动。"); //更新配置节
cc.UpdateConfig( "isFirstRunSystem", "false"); //更新配置节
this.Close();
Application.Restart();
}
catch (Exception ee)
{
MessageBox.Show( "配置失败\n" + ee.Message, "错误");
}
}
else
{
try
{
string str = string.Format( "data source ={0};initial catalog={1};user id={2};password={3};",
cbServerName.Text.Trim(), txtDBName.Text.Trim(), txtUsername.Text.Trim(), txtPassWord.Text);
Kitchen.Util.Config cc = new Kitchen.Util.Config();
cc.UpdateDBConfig( "ProConnectionString ", str, "System.Data.SqlClient");
if (MessageBox.Show( "更新成功,是否重新启动软件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Close();
Application.Restart();
}
else
{
this.Close();
}
}
catch (Exception ee)
{
MessageBox.Show( "配置失败\n" + ee.Message, "错误");
}
}
}
private void BindDataServer()
{
System.Data.Sql.SqlDataSourceEnumerator sdse = System.Data.Sql.SqlDataSourceEnumerator.Instance;
DataTable dt = sdse.GetDataSources();
cbServerName.DataSource = dt;
cbServerName.DisplayMember = "ServerName";
}
}
}