我正在尝试使用C#中的存储过程从MySQL数据库中获取数据,为了更精确地遵循三层体系结构我不确定下面的代码我在哪里错误代码我尝试了所有可能性小帮助将不胜感激很多。
DataServices Layer
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>
Store Procidure
CREATE DEFINER=`root`@`localhost` PROCEDURE `Storeprociduer`(
SchoolID
VARCHAR(255)
)
BEGIN
select UserName from scoolage_login where SchoolID =
SchoolID ;
END
答案 0 :(得分: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
}
}
}
也可以看到这个问题: