我尝试使用这个类与mysql db建立连接。
public class DataManager
{
// Connection String
public static string constr = ConfigurationManager.ConnectionStrings["cnn1"].ConnectionString;
public static DataSet GetDataSet(string stored_name, string table_name, params MySqlParameter[] prmarr)
{
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand(stored_name,con);
cmd.CommandType = CommandType.StoredProcedure;
foreach (MySqlParameter prm in prmarr)
{
cmd.Parameters.Add(prm);
}
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds,table_name);
return ds;
}
然后我在从母版页继承的页面的加载事件中调用它
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// product_select is stored procedure/ routine that I made it in db
using (DataSet ds = DataManager.GetDataSet("product_select","x")) // error is here
{
DataList1.DataSource = ds.Tables["x"];
DataList1.DataBind();
}
}
}
当我运行时,我得到这个错误。
An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
{"The given key was not present in the dictionary."}
我在xamppdb中的存储过程
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `product_select`()
NO SQL
SELECT 'P_id','name','unit_price','image_product' FROM product$$
DELIMITER ;
暂无答案!
目前还没有任何答案,快来回答吧!