我有一个带有 c# 和 mysql 的项目。对于查询,我准备了存储过程并使用
public Sorgulama sp_call(string sp_query, Dictionary<string, object> parameters)
{
Sorgulama sorgulama = new Sorgulama();
sorgulama.hatasiz = true;
DataTable dt = new DataTable();
using (var con = new MySqlConnection(myDBstring))
{
try
{
con.Open();
using (MySqlCommand cmd = new MySqlCommand(sp_query, con))
{
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null) {
foreach (KeyValuePair<string, object> item in parameters)
{
object value = new object();
cmd.Parameters.AddWithValue(item.Key, item.Value);
}
}
var rdr = cmd.ExecuteReader();
dt.Load(rdr);
rdr.Close();
}
}
catch (Exception Ex)
{
sorgulama.hatasiz = false;
///
string hatalirowlar = "";
foreach (DataRow dr in dt.GetErrors())
{
hatalirowlar = hatalirowlar + "\n" + dr.RowError.ToString();
}
XtraMessageBox.Show(hatalirowlar);
////
XtraMessageBox.Show("Bağlantı problemi." + Ex.ToString());
}
finally
{
if (con.State == System.Data.ConnectionState.Open) con.Close();
}
}
sorgulama.obje = dt;
return sorgulama;
}
这是我的存储过程示例:
SELECT h.id AS hesapid FROM `evrak_bordrolar` AS b
LEFT JOIN `hesaplar` AS h ON b.hesapid=h.id;
当某些行具有相同的值时,此过程在 phpmyadmin 或 sql 应用程序(如 Navicat)上没有错误。但它会在 c# 应用程序中引发约束异常。