我正在尝试从数据库中的表创建下拉列表,但出现错误:
不支持关键字:“提供者”
我想生成一个下拉列表,其中将包含我数据库中表城市中的城市,并将返回其中每个城市的索引。
connect.cs
班级
namespace RapidTyper.App_Code
{
public class connect
{
const string FILE_NAME = "DataBaseR1.accdb";
public static string GetConnectionString()
{
string location = HttpContext.Current.Server.MapPath("~/App_Data/" + FILE_NAME);
string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; data source=" + location;
return ConnectionString;
}
public connect()
{
// TODO: Add constructor logic here
}
public static void writeline()
{
throw new NotImplementedException();
}
}
}
页面:
<asp:DropDownList ID="ddlSubject" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="<Select Subject>" Value="0" />
</asp:DropDownList>
后面的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
LoadSubjects();
}
private void LoadSubjects()
{
DataTable cities = new DataTable();
using (SqlConnection con = new SqlConnection(connect.GetConnectionString()))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT cityNum, cityName FROM cities", con);
adapter.Fill(cities);
ddlSubject.DataSource = cities;
ddlSubject.DataTextField = "cityName";
ddlSubject.DataValueField = "cityNum";
ddlSubject.DataBind();
}
catch (Exception ex)
{
// Handle the error
}
}
}