解决方案 »

  1.   

    就是一个静态的属性,读取config配置文件的DbConfig节点的内容(从名称上面看应该返回一个数据库连接字符串)。
      

  2.   

    是的。
    config配置文件应该是这样的:<?xml version="1.0"?>
    <configuration>  <appSettings>
         ...
        <add key="DbConfig" value="User ID=xx;Password=xxx;Max Pool Size=512;Pooling=false;Data Source=xxx;"/>
        ...
      </appSettings></configuration>最终得到的值就是这个字符串:
    User ID=xx;Password=xxx;Max Pool Size=512;Pooling=false;Data Source=xxx;
      

  3.   

    C#连接数据库
    //选择SQL Server所有数据库下拉列表中的数据库刷新按钮事件
    private void button4_Click(object sender, EventArgs e)
            {
                string str = "server=" + textBox6.Text + ";database=master;Uid=" + textBox5.Text + ";Pwd=" + textBox4.Text + ";";
                comboBox1.DataSource = getTable(str);
                comboBox1.DisplayMember = "name";
                comboBox1.ValueMember = "name";
            }
    //获取数据库连接字符串
    private DataTable getTable(string str)
            {
                try
                {
                    SqlConnection sqlcon = new SqlConnection(str);
                    SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases ", sqlcon);
                    DataTable dt = new DataTable("sysdatabases");
                    da.Fill(dt);
                    return dt;
                }
                catch
                {
                    return null;
                }
            }
    //进行数据库连接操作
    private void button3_Click(object sender, EventArgs e)
            {
                if (radioButton1.Checked == true)
                {
                    if (textBox1.Text != "")
                    {
                        FileInfo FInfo = new FileInfo(textBox1.Text);
                        string strExtention = FInfo.Extension;
                        if (strExtention.ToLower() == ".mdb")
                        {
                            if (textBox2.Text != "")
                            {
                                strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";UID=" + textBox2.Text + ";PWD=" + textBox3.Text + ";";
                            }
                            else
                            {
                                strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";";
                            }
                        }//CodeGo.net/
                        else if (strExtention.ToLower() == ".xls")
                        {
                            strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended Properties=Excel 8.0;";
                        }
                    }
                    OleDbConnection oledbcon = new OleDbConnection(strCon);
                    try
                    {
                        oledbcon.Open();
                        richTextBox1.Clear();
                        richTextBox1.Text = strCon + "\n连接成功……";
                    }
                    catch
                    {
                        richTextBox1.Text = "连接失败";
                    }
                }
                else if (radioButton2.Checked == true)
                {
                    if (checkBox1.Checked == true)
                    {
                        strCon = "Data Source=" + textBox6.Text + ";Initial Catalog =" + comboBox1.Text + ";Integrated Security=SSPI;";
                    }
                    else if (checkBox2.Checked == true)
                    {
                        strCon = "Data Source=" + textBox6.Text + ";Database=" + comboBox1.Text + ";Uid=" + textBox5.Text + ";Pwd=" + textBox4.Text + ";";
                    }
                    SqlConnection sqlcon = new SqlConnection(strCon);
                    try
                    {
                        sqlcon.Open();
                        richTextBox1.Clear();
                        richTextBox1.Text = strCon + "\n连接成功……";
                    }
                    catch
                    {
                        richTextBox1.Text = "连接失败";
                    }
                }
            }
      

  4.   


    读取配置文件(当然之前你还要有配置文件加载的操作,然后要确保配置文件的appsettings下有对应DbConfig节点):
    ConfigurationManager.AppSettings["DbConfig"].ToString();   类的一个只读属性 返回连接串
    public static string Connstr