日期:2014-05-17  浏览次数:20990 次

C#数据库连接字符串放到配置文件出现问题
C#数据库连接字符串放到配置文件app.config后在conn.open处出问题,new SqlConnection(str)中的str改回"server=.\\SQL2005; database=clxdb;  integrated security=True"就没有问题,MessageBox.Show(str);显示server=.\\SQL2005; database=clxdb;  integrated security=True,求大神指点
app.config文件内容

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="abc" connectionString="server=.\\SQL2005; database=clxdb;  integrated security=True"/>
  </connectionStrings>
</configuration>

Button1按钮

        private void button1_Click(object sender, EventArgs e)
        {
            string str = ConfigurationManager.ConnectionStrings["abc"].ConnectionString;
            //
            using (SqlConnection conn = new SqlConnection(str))
            {MessageBox.Show(str);
                if(conn!=null && conn.State!=ConnectionState.Open)
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from manager";
                    using(SqlDataReader reader=cmd.ExecuteReader())
                    while (reader.Read())
                    {
                        string strreader=reader.GetString(0);
                        MessageBox.Show(strreader);
                    }