111,129
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;
namespace Vote
{
public partial class SysManage : Form
{
public SysManage()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection("server=" + textBox1.Text.Trim() + ";database=vote;uid=" + textBox2.Text.Trim() + ";pwd=" + textBox3.Text.Trim() + ";Connection Timeout=5;"))
{
try
{
connection.Open();
connection.Close();
System.Xml.XmlDocument str1 = new System.Xml.XmlDocument(); //创建一个xml文档模型
str1.Load(Application.StartupPath + @"\Config.xml"); //加载操作对象
System.Xml.XmlNode nodeServer = str1.SelectSingleNode("/Database/Server"); //获得节点
System.Xml.XmlNode nodeDatabaseName = str1.SelectSingleNode("/Database/DatabaseName");
System.Xml.XmlNode nodeUser = str1.SelectSingleNode("/Database/User");
System.Xml.XmlNode nodePassword = str1.SelectSingleNode("/Database/Password");
System.Xml.XmlNode nodeMode = str1.SelectSingleNode("/Database/mode");
nodeServer.InnerText = textBox1.Text.Trim(); // 为server节点里加文本内容
nodeDatabaseName.InnerText = "vote";
nodeUser.InnerText = textBox2.Text.Trim();
nodePassword.InnerText = textBox3.Text.Trim();
str1.Save(Application.StartupPath + @"\Config.xml"); //保存更新
//判断是否是由其他窗口打开,是则退出初始化
if (Application.OpenForms["Main"] != null || Application.OpenForms["KeyMain"] != null)
{
MessageBox.Show("配置修改需要重新启动程序才生效");
this.Close();
}
else
{
Main from = new Main();
from.MdiParent = null;
from.ShowDialog();
this.Close();
}
}
catch (System.Data.SqlClient.SqlException E)
{
MessageBox.Show("连接错误,请输入正确的连接参数");
}
finally
{
connection.Close();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void SysManage_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
if (!File.Exists(Application.StartupPath + @"\Config.xml"))
{
MessageBox.Show("配置文件损坏,重新安装");
return;
}
ds.ReadXml(Application.StartupPath + @"\Config.xml"); //用dataset读取xml
string Server = ds.Tables[0].Rows[0]["Server"].ToString().Trim(); //第一行 节点为server的 文本
string aseName = ds.Tables[0].Rows[0]["DatabaseName"].ToString().Trim();
string User = ds.Tables[0].Rows[0]["User"].ToString().Trim();
string Password = ds.Tables[0].Rows[0]["Password"].ToString().Trim();
string mode = ds.Tables[0].Rows[0]["mode"].ToString().Trim();
textBox1.Text = Server;
textBox2.Text = User;
textBox3.Text = Password;
if (mode == "0")
{
radioButton1.Checked = true;
radioButton2.Checked = false;
}
else
{
radioButton1.Checked = false;
radioButton2.Checked = true;
}
using (SqlConnection connection = new SqlConnection("server=" + Server + ";database=" + aseName + ";uid=" + User + ";pwd=" + Password + ";Connection Timeout=5"))
{
try
{
connection.Open();
connection.Close();
//判断是否是由其他窗口打开,是则退出初始化
if (Application.OpenForms["Main"] != null || Application.OpenForms["KeyMain"] != null)
{
return;
}
Main from = new Main();
from.ShowDialog();
from.MdiParent = null;
this.Close();
}
catch (System.Data.SqlClient.SqlException E)
{
}
finally
{
connection.Close();
}
}
}
}
}