日期:2014-05-20  浏览次数:20482 次

web.config 数据库连接出错
将   SqlConnection   myconn   =   new   SqlConnection(strConn);里的strConn直接用
"Server=ZEMU-A12\COMPANY;UID=zbr;PWD=#szzemucompany#;Database=dbok "   代替运行没有问题,但如果读取web.config中的 <add   key= "strConnString "   value= "Server=ZEMU-A12\COMPANY;UID=zbr;PWD=#szzemucompany#;Database=dbok "> </add>   中的strConnString值时则报     “实例失败”   的错误,请教原因
      protected   static   string   strConn   =   System.Configuration.ConfigurationManager.AppSettings[ "strConnString "];
               
                public   static   int   ExecuteSql(string   strSql)
                {
                        SqlConnection   myconn   =   new   SqlConnection(strConn);
                        SqlCommand   mycomm   =   new   SqlCommand(strSql,   myconn);
                        try
                        {
                                myconn.Open();
                                mycomm.ExecuteNonQuery();
                                return   0;
                        }
                        catch   (System.Data.SqlClient.SqlException   e)
                        {
                                throw   new   Exception(e.Message);
                        }
                        finally
                        {
                                mycomm.Dispose();
                                myconn.Close();
                        }
                }

Web.Config   文件内容:

<?xml   version= "1.0 "   encoding= "utf-8 "   ?>
<configuration>
    <appSettings>
        <add   key= "strConnString "   value= "Server=ZEMU-A12\COMPANY;UID=zbr;PWD=#szzemucompany#;Database=dbok "> </add>
        <add   key= "WebSiteVersion "   value= "Ver1.0 "> </add>
        <add   key= "WebManger "   value= "Windxf "> </add>
    </appSettings>
</configuration>