簡體   English   中英

使用Access數據庫ASP.NET C#出錯

[英]Getting error using access database asp.net c#

代碼修改SQL到MS Access數據庫。 這是我使用SQL數據庫的工作代碼,想要更改ms access數據庫但出現錯誤。 下面的工作代碼使用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

類別表已包含以下各列:

CategoryID CategoryName

產品表已包含以下各列:

PID圖像名稱產品名稱價格類別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訪問數據庫的代碼:

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();

    }
}

我正在使用嵌套的中繼器來按類別顯示產品。 它給數據集填充時間的錯誤。

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);

您似乎正在創建一個存儲過程,並在每次綁定數據時嘗試用它填充數據集。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM