简体   繁体   中英

Updating Database Using Dapper

I don't know if is this is possible in . Let's say I have this class:

public class Student
{
    public int Id{get;set;}
    public string FirstName{get;set;}
    public string LastName{get;set;}

    public int UpdateStudent(Student student)
    {
        string sql="Update Student set FirstName=@FirstName, LastName=@LastName where Id=@Id";
        Dapper.Execute(sql, student)
    }
}

Now, on the calling code, I will have something like this:

Student student=new Student();
student.Id=1;
student.FirstName="abc";
student.UpdateStudent(student);

Now, if we are going to update the Student and only provide Id and FirstName , it will throw an error that I will also need to provide the LastName . I am looking for a solution that can be use that even if if I do not specify the Student.LastName , it will still do the update and since I don't specify the LastName , the Student.LastName will remain the same.

Ok here is a quick and dirty:

I did not compile or test this so it might have a typo

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);
  }

Call it like this

  UpdateFromItem("Student", new { FirstName : "abc"}, new { Id : 1 });

I made a generic solution above, you could also "know" about the id field for student object specifically then a solution like this would work:

  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);
  }

People will point out that this is has injection risk. That can be solved by composing a parameter list -- but I think you get the idea. Basically you need a loop.



This isn't hard you will need an if statement, something like this:

if (student.lastName.IsNullOrEmpty())
  Dapper.Execute("Update Student set FirstName=@FirstName where Id=@Id", student);
else
  Dapper.Execute("Update Student set FirstName=@FirstName, LastName=@LastName where Id=@Id", student);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM