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>
[解决办法]
<add key= "strConnString " value= "Server=ZEMU-A12\COMPANY;UID=zbr;PWD=#szzemucompany#;Database=dbok "> </add>
后面的 </add> 直接换成/> 试试看
[解决办法]
protected static string strConn = System.Configuration.ConfigurationManager.AppSettings[ "strConnString "];
换成
protected static string strConn = System.Configuration.ConfigurationSettings.AppSettings[ "strConnString "];
试试
[解决办法]
是不是,传入日sql 语句有问题?
[解决办法]
protected static string strConn = System.Configuration.ConfigurationSettings.AppSettings[ "strConnString "];
同意这个方法但是要引入一个命名空间,查一下就知道了
[解决办法]
strConn=ConfigurationSettings.AppSettings( "strConnString ")
[解决办法]
<configuration>
<appSettings>
<add key= "conString "
value= "Server=localhost;UID=sa;PWD=secret;Database=Northwind " />
</appSettings>
</configuration>
-------------------------
string strConString = ConfigurationSettings.AppSettings[ "conString " ];
[解决办法]
<configuration>
<appSettings>
<add key= "msn " value= "server=127.0.0.1;uid=sa;pwd=sa;database=BookShop " />
</appSettings>
</configuration>
string strConString = ConfigurationSettings.AppSettings[ "msn " ];
也许 这个行,你试试吧!
[解决办法]
<appSettings>
<add key= "connstr " value= "server=.;uid=zbr;pwd=#szzemucompany#;database=dbok " />
</appSettings>
--------------------
public static string ConnStr=System.Configuration.ConfigurationSettings.AppSettings[ "ConnStr "];
如果再不行,就不是这方面的问题.
[解决办法]
ding
[解决办法]
using System.Configuration;
public static int ExecuteSql(string strSql)
{
SqlConnection myconn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "ConnStr "]);
SqlCommand mycomm = new SqlCommand(strSql, myconn);
Try