一般的方法为在Web.config的<connecttionString>的写连接字符串,如:
"server=127.0.0.1;database=northwind;user id=sa;password=1234;";然后通过在page_load获取它
比如获取后保存在变量中ls_conn :protected void Page_Load(object sender, EventArgs e)
{
string ls_conn = "server=127.0.0.1;database=northwind;user id=sa;password=1234;";
}
但是我在ASPX文件中怎么样引用呢,我的引用方法如下:
在aspx文件中引用,
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT * FROM Customers"
ConnectionString="<% =ls_conn %>"
runat="server" ProviderName="System.Data.SqlClient" >
</asp:sqldatasource> 在运行时报错误:
System.ArgumentException: 不支持关键字: “<%”。=======================================================大家有没有这样用过啊,这种方法调用时一般怎么处理啊?
如果用cs文件通过fill(),bind()来写当然没有问题.我想知道在aspx文件中怎么实现.
"server=127.0.0.1;database=northwind;user id=sa;password=1234;";然后通过在page_load获取它
比如获取后保存在变量中ls_conn :protected void Page_Load(object sender, EventArgs e)
{
string ls_conn = "server=127.0.0.1;database=northwind;user id=sa;password=1234;";
}
但是我在ASPX文件中怎么样引用呢,我的引用方法如下:
在aspx文件中引用,
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT * FROM Customers"
ConnectionString="<% =ls_conn %>"
runat="server" ProviderName="System.Data.SqlClient" >
</asp:sqldatasource> 在运行时报错误:
System.ArgumentException: 不支持关键字: “<%”。=======================================================大家有没有这样用过啊,这种方法调用时一般怎么处理啊?
如果用cs文件通过fill(),bind()来写当然没有问题.我想知道在aspx文件中怎么实现.
{
string ls_conn = "server=127.0.0.1;database=northwind;user id=sa;password=1234;";
}
-------------
这样做和web.config内的有什么关系???
-------------
建个数据库访问的类,然后设置GridView的数据源的时候调用这个类中的方法,
比如数据库访问类(DAL)有个方法是获取产品表的(GetProducts)
直接在cs中this.GridView1.DataSource=DAL.GetProducts();
this.GridView1.DataBind();
---
在GetProducts中去设置数据连接等东西
在CS页面里引用using System.Configuration
System.Configuration.ConfigurationSettings.AppSettings["xxxxx"];
<connectionStrings>
<add name="TaxHouseConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TaxHouse.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
(2)在sqlobjectdatasource中,注意在生成时选择上面的连接字符串,这个是不需要自已写的:<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TaxHouseConnectionString %>"
SelectCommand="SELECT [KeyID], [DeptID] FROM [Biz_HouseInfo]"></asp:SqlDataSource>
(3)在程序代码中,注意写法与2003不同,以下是标准写法,当然2003中的也能用:
string conncetString = System.Configuration.ConfigurationManager.ConnectionStrings["TaxHouseConnectionString"].ToString();
Response.Write(conncetString);
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT * FROM Customers"
ConnectionString="<%# ls_conn %>"
runat="server" ProviderName="System.Data.SqlClient" >
</asp:sqldatasource>
可能是我表述不够清楚,其他兄弟的方法是可以的,但是不是我现在需要的.