SQL Server Dapper更新列不同的行

时间:2018-04-02 03:56:20

标签: c# sql dapper

我正在编写一个开源应用程序(https://github.com/J-east/WellsFargoPDFTaxExtractor),用于将银行对帐单PDF导入到sql server db实例中。我想要一个简单的方法来获取事务对象并更新事务数据库。请参阅以下代码:

    public class Transaction {
        public int TransactionID;
        public long accountNumber;
        public DateTime TransDate;
        public string Title;
        public string Summary;
        public string catagory;
        public string typeOfTransaction;
        public double amount;
    }

    public static bool UpdateRow(Transaction t) {
        string sql = "update TRANSACTIONS SET ???? where TransactionID = @transactionID";

        // code to take t and update the rows where TransactionID = @transactionID and things are different

        return true;
    }

编辑: 感谢@Loofer,我发现了这个非常酷的工具:DapperContrib

    [Table("Transactions")]
    public class TransactionContrib {
        [Key]
        public int TransactionID { get; set; }

        public long AccountNumber { get; set; }
        public DateTime TransDate { get; set; }
        public string Title { get; set; }
        public string Summary { get; set; }
        public string Catagory { get; set; }
        public string TypeOfTransaction { get; set; }
        public double Amount { get; set; }
    }

    public static bool UpdateRow(TransactionContrib t) {
        string conStr;
        if (Program.Settings.SqlSettings.IntegratedSec) {
            conStr = $"SERVER={Program.Settings.SqlSettings.server};DATABASE={Program.Settings.SqlSettings.database};Integrated Security = SSPI;";
        }
        else {
            conStr = $"SERVER={Program.Settings.SqlSettings.server};DATABASE={Program.Settings.SqlSettings.database};UID={Program.Settings.SqlSettings.userID};PWD={sqlpw}";
        }
        using (SqlConnection connection = new SqlConnection(conStr)) {
            return connection.Update(t);
        }            
    }

1 个答案:

答案 0 :(得分:0)