c# - Sending an update statement to sql-server using Dapper without all properties having values -
i have simple update function in data access layer uses dapper , updates product.
public static void updateproduct(product product) { using (var connection = getopenconnection()) { stringbuilder updatesql = new stringbuilder(); updatesql.appendline("update proformaassumption "); updatesql.appendline("set categoryid = @categoryid "); updatesql.appendline(" ,purchasedate = @purchasedate "); updatesql.appendline(" ,type = @type "); updatesql.appendline("where id = @id;"); connection.execute(updatesql.tostring(), product); } }
lets have create product scratch in code , want update existing product based on , know existing product's id. want update category, not other fields object not have fields set.
product product = new product(); product.id = 325; product.categoryid = 16; productdal.updateproduct(product);
i getting 2 errors: purchasedate: error - sqldatetime overflow. must between 1/1/1753 12:00:00 , 12/31/9999 11:59:59 pm
, cannot insert null value type
i need find way tweak update function update items values. have wrap if statement around each appendline like:
if (product.type != null) { updatesql.appendline(" ,type = @type "); }
i not sure if problem dapper related or sending values sql c# in general. thought dapper made things easy.
Comments
Post a Comment