使用Npgsql库调用PostgreSQL的函数(存储过程)

前端之家收集整理的这篇文章主要介绍了使用Npgsql库调用PostgreSQL的函数(存储过程)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Postgresql没有像sqlServer一样的存储过程。他们的标准也不一样,sqlserver是T-sql标准,而Postgresql有很多过程语言,其中以 PL/pgsql最热。由于我也是初学,就不在这里跟大家讨论语法了。进入主题,模拟实现sqlserver中的存储过程

首先,在建好Postgresql中的函数(Postgresql中过程用函数来实现),代码如下:

CREATE OR REPLACE FUNCTION "public"."userreg" (p_passport varchar,p_nickname varchar,out p_password varchar,out p_userid integer) RETURNS record AS
$body$
declare
tmpstr varchar(32);
begin
$3 := NULL;
$4 := NULL;
if exists(select 1 from public.t_user where passport = $1) then
begin
$4:=-1;
end;
else
begin
$3 := cast ((round((random()+1) * 9999999)) as varchar(20));
tmpstr := md5($3);
insert into public.t_user (passport,password,nickname) values($1,tmpstr,$2);
$4:=(select userid from public.t_user where passport = $1);
end;
end if;
EXCEPTION
WHEN others THEN
$4:=-2;
$3:=-2;
en d;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

以上示例代码,模拟sqlServer,所以故意加入了两个In参数和两OUT参数。

下面是在.net下的调用代码,就跟sql中的存储过程调用一样。ODBC方式貌似不行。

view plaincopy to clipboardprint?public void UserRegiste(T_User user) { NpgsqlConnection conn = new NpgsqlConnection(); conn.ConnectionString = "Server=127.0.0.1;Port=5432;Userid=postgres;database=jtnet;password=123456;Protocol=3;SSL=false;Pooling=true;MinPoolSize=1;MaxPoolSize=20;Encoding=UNICODE;Timeout=60;SslMode=Disable"; NpgsqlCommand cmd = new NpgsqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "userreg"; NpgsqlParameter p1 = new NpgsqlParameter("p_passport",DbType.String,50); NpgsqlParameter p2 = new NpgsqlParameter("p_nickname",50); NpgsqlParameter p3 = new NpgsqlParameter("p_nickname",DbType.Int32); NpgsqlParameter p4 = new NpgsqlParameter("p_userid",DbType.Int32); p1.Value = user.Passport; p2.Value = user.UserNickName; p3.Direction = ParameterDirection.Output; p4.Direction = ParameterDirection.Output; cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); cmd.Parameters.Add(p3); cmd.Parameters.Add(p4); conn.Open(); NpgsqlDataReader dr = cmd.ExecuteReader(); dr.Read(); //此处可以写上显示代码 } public void UserRegiste(T_User user) { NpgsqlConnection conn = new NpgsqlConnection(); conn.ConnectionString = "Server=127.0.0.1;Port=5432;Userid=postgres;database=jtnet;password=123456;Protocol=3;SSL=false;Pooling=true;MinPoolSize=1;MaxPoolSize=20;Encoding=UNICODE;Timeout=60;SslMode=Disable"; NpgsqlCommand cmd = new NpgsqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "userreg"; NpgsqlParameter p1 = new NpgsqlParameter("p_passport",50); NpgsqlParameter p2 = new NpgsqlParameter("p_nickname",50); NpgsqlParameter p3 = new NpgsqlParameter("p_nickname",DbType.Int32); NpgsqlParameter p4 = new NpgsqlParameter("p_userid",DbType.Int32); p1.Value = user.Passport; p2.Value = user.UserNickName; p3.Direction = ParameterDirection.Output; p4.Direction = ParameterDirection.Output; cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); cmd.Parameters.Add(p3); cmd.Parameters.Add(p4); conn.Open(); NpgsqlDataReader dr = cmd.ExecuteReader(); dr.Read(); //此处可以写上显示代码

原文链接:https://www.f2er.com/postgresql/197036.html

猜你在找的Postgre SQL相关文章