用XML来配置好了,把XML文件放在Debug下
<?xml version="1.0" encoding="utf-8"?>
<QC>
<APP>
<DATACONNECT comment="数据库连接字符串">Data Source=localhost ;Initial Catalog=CC;User ID=sa;Pwd=123456;Integrated Security=True;</DATACONNECT>
</APP>
</QC>
我习惯把配置的信息赋值到textBox中,方便后面写,加入有需要,也可以跳过,来连接数据库的时候直接读取XML文件
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument configdoc = new XmlDocument();
configdoc.Load(Application.StartupPath + "\\config.xml");
textBox1.Text = configdoc.SelectSingleNode("/QC/APP/DATACONNECT").InnerText;
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
try
{
connection = new SqlConnection(textBox1.Text);
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
= "Select * from Gather";
。。。。。。。
。。。。。。。
}