gpt4 book ai didi

c# - 获取 asp :SqlDataSource Connection String dynamically from a method (Without Webconfig)

转载 作者:行者123 更新时间:2023-12-05 04:12:31 24 4
gpt4 key购买 nike

尝试从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com