2015-11-06 57 views
1

我想在C#中使用存儲過程從MySQL數據庫中獲取數據,更確切地說,我遵循以下三層架構代碼我已經嘗試過所有的可能性小的幫助將不勝感激。Asp.net C#Mysql使用存儲過程獲取數據表格數據庫

DataService的層

public DataSet FetchLoginDetails(string SchoolID) 
    { 
     object[] objparam = new object[1]; 
     objparam[0] = new MySqlParameter 
     { 
      ParameterName = "@SchoolID", 
      DbType = System.Data.DbType.String, 
      Value =SchoolID 
     }; 

     DataSet dsdataRes = ExecuteQuery("Storeprociduer", objparam); 
     return dsdataRes; 

    } 

BusinessServices

private ITimeTableRepository ObjTimeTable 
    { 
     get { return UnityManager.Resolve<ITimeTableRepository>(); } 
    } 
    public DataSet FetchLoginDetails(string SchoolID) 
    { 
     return ObjTimeTable.FetchLoginDetails(SchoolID); 
    } 

BusnessServices.Interfaces

public interface ITimeTableBO 
    { 
     DataSet FetchLoginDetails(string SchoolID); 
    } 

UI的.cs

public void LoadLoginDetails(DataSet ds) 
    { 
     if (ds.Tables.Count > 0) 
     { 
      if (ds.Tables[0].Rows.Count > 0) 
      { 
       ddlSubject.Items.Clear(); 

       ListItem item1 = new ListItem(); 
       item1.Text = "Choose a Subject"; 
       item1.Value = "-1"; 
       ddlSubject.Items.Add(item1); 
       for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
       { 

        ListItem item = new ListItem(); 
      item.Text = ds.Tables[0].Rows[i]["UserName"].ToString(); 
      item.Value = ds.Tables[0].Rows[i]["UserName"].ToString(); 
        ddlSubject.Items.Add(item); 
       } 
      } 

.aspx的代碼

     <asp:DropDownList ID="ddlSubject" runat="server" Width="150px" 

Height="27px" ClientIDMode="Static" 
          AutoPostBack="true" > 
          <asp:ListItem Value="-1" Text="Choose a Subject"></asp:ListItem> 
         </asp:DropDownList> 

商店Procidure

CREATE DEFINER=`root`@`localhost` PROCEDURE `Storeprociduer`(
SchoolID 

VARCHAR(255) 
) 
BEGIN 

select UserName from scoolage_login where SchoolID = 

SchoolID ; 


END 
+0

您還沒有指定哪個存儲過程調用FetchLoginDetails。 – Drew

+0

@Drew對不起,這是我的錯誤,而在我的代碼發佈我已經提到了商店的程序名稱,但它仍然沒有顯示 – Shaik

回答

1

不看你的代碼的其他細節,我假設你是無法連接和檢索你的MySQL數據庫中的數據。看看MySqlCommand對象。有代碼示例,以及:

https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-sql-command.html

嘗試是這樣的:

using (MySqlConnection con = new MySqlConnection(yourConnectionString)) 
    { 
     using (MySqlCommand cmd = new MySqlCommand("Storeprociduer", con)) 
     { 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@SchoolID", SchoolID); 
      using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd)) 
      { 
       DataTable dt = new DataTable(); 
       sda.Fill(dt); 
       // Do something with results 
      } 
     } 
    } 

參見這太問題還有:

calling mysql storedprocedure from c#?

+0

德魯感謝您的支持,如果你沒有問題,我可以通過團隊查看器分享我的屏幕你可以看看會更精確 – Shaik

+0

嗨Shaik,我已經添加了一個應該幫助你開始的例子。如果問題得到解決,請接受答案。 – Drew

相關問題