我正在使用postgres11,并且创建了一个小的过程来将数据插入到表中。在我的C#应用程序中,我使用带有dapper的Npgsql来调用该过程,我的代码运行正常,但dapper的Execute返回-1。
当将dapper与SQL SERVER一起使用时,dapper的DynamicParameter可以很好地工作,但是在这里似乎没有用。
我也对我的代码不满意。有什么更好的方法可以编写以下代码。
这是我的程序:
CREATE OR REPLACE PROCEDURE public.uspcreatedepartment(
deptname character varying,
isactive boolean)
LANGUAGE 'sql'
AS $BODY$
insert into "tblDepartment"("DeptName", "IsActive")
values (deptName, isActive);
$BODY$;
C#代码:
public bool DepartmentCrud(Department department)
{
NpgsqlConnection con = new
NpgsqlConnection("my connection string");
con.Open();
var dynamicParam = new DynamicParameters();
dynamicParam.Add("deptname",
department.DeptName,
DbType.String,
ParameterDirection.Input);
dynamicParam.Add("isactive",
department.IsActive,
DbType.Boolean,
ParameterDirection.Input);
//i am not happy with my below code
//How do i pass parameter to my procedure in a better way ?
string deptName = "'" +
string.Join("','",
department.DeptName) +
"'";
//I am not happy here as well since too many null in my
// con.Execute
//Also i would like to know why this con.Execute returning me -1
// ??
//Is there any better way to write this dapper code by calling
// that postgres procedure
//which is accepting some parameters ??
var test = con.Execute("CALL uspCreateDepartment(" + deptName + "," + department.IsActive + ")",
null,
null,
null,
null);
return test == 1
? true
: false;
}
有人可以建议我用这种精致的工具调用postgres过程的最佳方法吗?谢谢。