OK]をクリックして迅速かつ汚いです:
それはタイプミス
public static void UpdateFromItem(string tableName, object updatevalues, object selectorvalue)
{
string updateStr=new String();
string whereStr=new String();
foreach (PropertyInfo prop in updatevalues.GetType().GetProperties())
{
if (prop.GetValue(parms, null) != null)
updateStr.AppendFormat(" %s=%s",prop.Name, prop.GetValue(parms, null));
}
foreach (PropertyInfo prop in selectorvalues.GetType().GetProperties())
{
if (prop.GetValue(parms, null) != null)
updateStr.AppendFormat(" %s=%s",prop.Name, prop.GetValue(parms, null));
}
string sqlStmt=string.Format(@"UPDATE %s SET %s WHERE %s",tableName, updateStr,wherreStr);
Drapper.Execute(sqlStmt);
}
この
UpdateFromItem("Student", new { FirstName : "abc"}, new { Id : 1 });
のようにそれを呼び出している場合がありますので、私はこれをコンパイルするか、テストしていません
私は上記の一般的なソリューションを作った、あなたはまた、学生オブジェクトのidフィールドについて、具体的にこのような解決策はうまくいくでしょう:
public static void UpdateStudent(Student inobj)
{
string updateStr=new String();
string whereStr=string.Format(@"Id=%s",inobj.Id);
foreach (PropertyInfo prop in inobj.GetType().GetProperties())
{
if ((prop.GetValue(parms, null) != null) && (prop.Name != 'Id'))
updateStr.AppendFormat(" %s=%s",prop.Name, prop.GetValue(parms, null));
}
string sqlStmt=string.Format(@"UPDATE %s SET %s WHERE %s",tableName, updateStr,wherreStr);
Drapper.Execute(sqlStmt);
}
人々は、これは注射リスクがあることを指摘します。それはパラメータリストを作成することで解決できますが、あなたはその考えを得ると思います。基本的にはループが必要です。
これはあなたが必要となります難しいことではありませんif文、このような何か:
if (student.lastName.IsNullOrEmpty())
Dapper.Execute("Update Student set [email protected] where [email protected]", student);
else
Dapper.Execute("Update Student set [email protected], [email protected] where [email protected]", student);
あなたの質問が何であるか全く分かりません。 – Hogan
混乱して申し訳ありません。私は質問を編集してみんなに分かりやすくします。 –
IF文を使用する必要があります – Hogan