我有一个学校项目即将推出,它是ASP.NET中的一个Web应用程序,我认为我已经完成并使用本地MySQL数据库按预期工作。我的教授最近指示我们将我们的数据库更改为MS Access,并且我已经成功地将切换全部用于我的课程之一。有问题的类从我的数据库表中注册人员,并在网格视图中列出,使用过滤器并将DOB转换为年龄。我将包含我的代码以及错误的屏幕截图。从环顾四周我发现你可以配置一个SqlDataSource来使用访问数据库,所以我按照步骤操作,这是我现在得到的错误; '在应用程序配置中找不到连接名称'newregDonneonnectionString.System.Data.OleDb',或者连接字符串为空。'
任何人都可以帮我看看我哪里出错了吗?提前谢谢!
我的Web.config;
<add name="newregDBConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Users\Alex\Documents\Database1.accdb"
providerName="System.Data.OleDb" />
我使用访问数据库配置SqlDataSource的步骤;
<asp:SqlDataSource
ID="source"
runat ="server"
ConnectionString ="<%$ ConnectionStrings:newregDBConnectionString %>"
ProviderName ="<%$ ConnectionStrings:newregDBConnectionString.System.Data.OleDb %>"
SelectCommand= "SELECT firstname, childID, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children ORDER BY age" />
我正在尝试显示gridview的C#代码;
namespace Coursework
{
public partial class Testy1 : System.Web.UI.Page
{
//create a datasource
SqlDataSource source = new SqlDataSource();
protected void Page_Load(object sender, EventArgs e)
{
//always set some defaults for the datasource
source.ID = "source1";
source.ConnectionString = ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString;
source.SelectCommand = "SELECT firstname, childID, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children ORDER BY age";
if (!IsPostBack)
{
//bind the grid
GridView1.DataSource = source;
GridView1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//the new database query, now with where clause
source.SelectCommand = "SELECT firstname, childID, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children WHERE (DATEDIFF(hour, dob, GETDATE()) / 8766 BETWEEN @start AND @end) ORDER BY age";
//get the end age from the dropdown and cast as int
int end = Convert.ToInt32(DropDownList1.SelectedValue);
//get the start int for the filter
int start = end - 2;
//if the filter is resetted, make sure the query returns all ages
if (end == 5)
{
start = 5;
end = 99;
}
//replace the parameters in the query
source.SelectParameters.Add("start", start.ToString());
source.SelectParameters.Add("end", end.ToString());
//rebind the grid
GridView1.DataSource = source;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string childID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string deleteSql = "DELETE FROM Children WHERE childID = @childID; ";
using (var con = new OleDbConnection(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString))
using (var cmd = new OleDbCommand(deleteSql, con))
{
cmd.Parameters.Add("@childID", OleDbType.VarChar).Value = childID;
con.Open();
int deleted = cmd.ExecuteNonQuery();
}
GridView1.DataSource = source;
GridView1.DataBind();
}
protected void backBtn_Click(object sender, EventArgs e)
{
Response.Redirect("Registration.aspx");
}
}
}
答案 0 :(得分:0)
ProviderName
中的SqlDataSource
属性值使用的格式错误:
ProviderName="<%$ ConnectionStrings:newregDBConnectionString.System.Data.OleDb %>"
来自MSDN documentation,providerName
数据源绑定中的ConnectionStrings:newregDBConnectionString.providerName
属性是指web.config连接字符串元素中的providerName
属性值。因此,您应该保留providerName
属性:
<asp:SqlDataSource ID="source" runat="server"
ConnectionString="<%$ ConnectionStrings:newregDBConnectionString %>"
ProviderName="<%$ ConnectionStrings:newregDBConnectionString.providerName %>"
SelectCommand="SELECT firstname, childID, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children ORDER BY age" />
或直接使用提供商名称:
ProviderName="System.Data.OleDb"
相关问题(请参阅以类似方式使用providerName
属性的方式):
ASP .NET - Configure SQLDataSource to use MySQL .NET Connector