代码修改SQL到MS访问数据库。 这是我使用SQL数据库的工作代码,并希望更改ms访问数据库但收到错误。下面的工作代码使用sql数据库:
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
SqlConnection myConnection = new SqlConnection("Data Source=Mazhar-PC;Initial Catalog=MKE;user id=sa;password=ok; ");
SqlCommand myCommand = new SqlCommand("usp_GetProductsForCategories", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ad = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
ad.Fill(ds);
// Attach the relationship to the dataSet
ds.Relations.Add(new DataRelation("CategoriesRelation", ds.Tables[0].Columns["CategoryID"],
ds.Tables[1].Columns["CategoryID"]));
outerRep.DataSource = ds.Tables[0];
outerRep.DataBind();
}
protected void outerRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
Repeater innerRep = e.Item.FindControl("innerRep") as Repeater;
innerRep.DataSource = drv.CreateChildView("CategoriesRelation");
innerRep.DataBind();
}
}
我使用了两张桌子:
Category
Product
类别表一直是以下列:
类别ID 类别名称
产品表一直是以下列:
PID ImageName 产品名称 价钱 类别ID
我在sql中有一个存储过程,可以根据类别及其产品获得结果。
ALTER PROCEDURE [dbo].[usp_GetProductsForCategories]
AS
SELECT *
FROM Category
WHERE CategoryID IN (SELECT CategoryID FROM Product) SELECT p.PID, p.ImageName, p.ProductName, p.Price, p.CategoryID FROM Product p
下面没有使用ms access database的代码:
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\LKKT1\My Documents\Downloads\examples\demo1\kkk.accdb";
conn.Open();
string sql = "create procedure usp_GetProductsForCategories as select * from Category3 where CategoryID IN (select CategoryID from Product3) SELECT p.PID, p.ImageName, p.ProductName, p.Price, p.CategoryID FROM Product3 p
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter ad=new OleDbDataAdapter(cmd);
DataSet ds=new DataSet();
ad.Fill(ds);
ds.Relations.Add(new DataRelation("CategoriesRelation",ds.Tables[0].Columns["CategoryID"],
ds.Tables[1].Columns["CategoryID"]));
outerRep.DataSource=ds.Tables[0];
outerRep.DataBind();
}
protected void outerRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
Repeater innerRep = e.Item.FindControl("innerRep") as Repeater;
innerRep.DataSource = drv.CreateChildView("CategoriesRelation");
innerRep.DataBind();
}
}
我正在使用嵌套转发器根据类别显示产品。它给出了数据集填充时间的错误。
答案 0 :(得分:1)
string sql = "select * from Category3
where CategoryID IN (select CategoryID from Product3) SELECT p.PID, p.ImageName,
p.ProductName, p.Price, p.CategoryID FROM Product3 p";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter ad=new OleDbDataAdapter(cmd);
DataSet ds=new DataSet();
ad.Fill(ds);
您似乎正在创建存储过程,并在每次绑定数据时尝试使用它填充数据集。