![](/img/trans.png)
[英]How to set connectionstring at web.config file programmatically or dynamically?
[英]how to write connectionstring in web.config file
我的代码运行良好,但是尝试托管之后。 其数据库始终以null值响应。 我无法托管。 现在,当我尝试在PC上进行调试时,它也会遇到null响应相同的问题。
我的类文件及其标量查询代码。
public Object ExecuteScalarQuery(String sp)
{
String _ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["rfid"].ConnectionString;
// string _ConnString = ConfigurationManager.ConnectionStrings["rfid"].ConnectionString;
SqlConnection myConnection = new SqlConnection(_ConnString);
SqlCommand cmd = new SqlCommand(sp, myConnection);
Object result = 0;
try
{
myConnection.Open();
result = cmd.ExecuteScalar();
}
catch (Exception ex)
{
//if (myConnection.State == ConnectionState.Open)
// myConnection.Close();
}
finally
{
//if (myConnection.State == ConnectionState.Open)
// myConnection.Close();
}
return result;
}
和具有连接字符串的web.config文件
<connectionStrings>
<add name="rfid" connectionString="Data Source=CHINTAN-PC\SQLEXPRESS;Initial Catalog=msdb;Integrated Security=True " providerName="System.Data.SqlClient"/>
在逐步调试其连接字符串时看起来像这样,这是行不通的。
"Data Source=CHINTAN-PC\\SQLEXPRESS;Initial Catalog=msdb;Integrated Security=True "
要检查的一件事是存储过程正在返回结果。 我复制了您的代码,创建了一个表和一个存储过程以从中查询所有记录,当表为空时,它返回null,当我添加几个记录时,它返回第一行第一列的值。
添加属性“池”。
<add name="rfid" connectionString="Data Source=CHINTAN-PC\SQLEXPRESS;Initial Catalog=msdb;Integrated Security=True; pooling=false;" providerName="System.Data.SqlClient"/>
转到服务器资源管理器,选择您的数据库。 在右窗格(属性)中复制连接字符串并将其粘贴。 如果那不起作用。 去SQL管理工作室。 它是您的Windows身份验证和sql身份验证问题。 进行新的sql身份验证登录,并在连接字符串中输入userid =“”和password =“”,例如“ sa”和“ sa123”。
使用toString检索字符串值
String _ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["rfid"].ConnectionString.toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.