在sqldatasource中调用connectionstring

时间:2016-08-29 08:30:19

标签: sql-server c#-4.0

如何从类中调用Sqldatasource中的连接字符串而不是直接写入?

    <asp:SqlDataSource  ID="sqldatasource" runat="server" ConnectionString="..." SelectCommand="sp"    SelectCommandType="StoredProcedure">    

    </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:0)

public class CXXX { 
     public String ConnectionString() { 
           return "data source=.\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True;MultipleActiveResultSets=True;Pooling=false;"; 
        } 
}

声明一个对象

protected CXXX _myObject;

然后,在Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
         _myObject = new CXXX();
}

然后,在default.aspx中,你可以这样做:

<asp:SqlDataSource  ID="sqldatasource" runat="server" ConnectionString="<%= CXXX .ConnectionString() %>" SelectCommand="sp"    SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

答案 1 :(得分:0)

写代码背后的代码

  SqlDataSource1.ConnectionString=Class1.StringConnectionObj;

http://www.codeproject.com/Questions/1121212/How-do-I-call-connection-string-in-sqldatasource-f