作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试从 asp:sqldatasource 标记中的类设置 sql server:
Public static SqlConnection getConnection()
{
SqlConnection conn = new SqlConnection();
//rest of the code that 100% works
return conn;
}
此功能已在 C# 代码中进行测试,并且 100% 有效。现在我正尝试在 asp:SqlDataSource 中使用它:
<asp:SqlDataSource runat="server" ID="sqlDBConnection"
ConnectionString="<%# inProcessInventory.DataTools.getConnection().ConnectionString %>"
我收到:
The ConnectionString property has not been initialized
有什么建议吗?
最佳答案
顾名思义,ConnectionString 是一个字符串。您正在返回一个 SqlConnection
,其中需要一个字符串。
public static string getConnection()
{
return "Data Source=localhost;Initial Catalog=yourDB;User ID=yourUser;Password=pa$$w0rd";
}
或者
public static SqlConnection getConnection()
{
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=yourDB;User ID=yourUser;Password=pa$$w0rd");
return conn;
}
更新
如果您在后面的代码中设置连接字符串,它确实可以正常工作:
protected void Page_Load(object sender, EventArgs e)
{
sqlDBConnection.ConnectionString = inProcessInventory.DataTools.getConnection().ConnectionString;
}
关于c# - 获取 asp :SqlDataSource Connection String dynamically from a method (Without Webconfig),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40190834/
我是一名优秀的程序员,十分优秀!