前置阅读文章
《EFCore使用连接GBase8s数据库示例》
《C#连接GBase8s数据库在windows环境下使用步骤》
创建解决方案Demo工程,并引入相应的EFCore包
我们首先创建名字为test的数据库,并创建使用到的数据表和存储过程。
示例1: 有两个入参和两个返回值的存储过程
CREATE TABLE test:company (
id INTEGER,
branck VARCHAR(100),
address VARCHAR(100),
city VARCHAR(100),
phone VARCHAR(100)
)
create procedure testselect (id int,city varchar(100)) returning varchar(100),varchar(100)
begin
define v_in int;
define v_in2 VARCHAR(100);
let v_in = id+1;
let v_in2 = “Hello GBase8s”;
insert into company values (id,‘1’,‘2’,city,‘3’);
return 'value: ’ || v_in,v_in2 ;
end
end procedure;
控制台输出结果,与预期一致,两个返回值
数据也insert成功。
示例代码:
GbsConnection conn = new GbsConnection(builder.ConnectionString);conn.Open();GbsCommand cmd = conn.CreateCommand();mandText = "testselect";mandType = mandType.StoredProcedure;cmd.Parameters.Add(new GbsParameter("@id", SqlDbType.VarChar));cmd.Parameters["@id"].Value = 44;cmd.Parameters.Add(new GbsParameter("@city", SqlDbType.Int));cmd.Parameters["@city"].Value = "SH";cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;cmd.ExecuteNonQuery();GbsDataReader dr = (GbsDataReader)cmd.ExecuteReader();while (dr.Read()){for (int i = 0; i < dr.FieldCount; i++){Console.WriteLine(dr[i]);}}
示例2: 有一个出参的存储过程。
create procedure call_out_param() returning varchar(100)
begin
define v_in int;
define v_rc varchar(100);
let v_in=-1;
call out_param(p_out = v_in) returning v_rc;
let v_rc = v_rc || ‘, value :’ || v_in;
return v_rc;
end
end procedure;
create procedure out_param (out p_out int) returning varchar(100)
begin
define v_in int;
let v_in =1;
let p_out = v_in+1;
return ‘value:’|| v_in;
end
end procedure;
执行结果
与存储过程执行结果一致。
示例代码
GbsConnection conn = new GbsConnection(builder.ConnectionString);
conn.Open();
GbsCommand cmd = conn.CreateCommand();
mandText = “call_out_param”;
mandType = mandType.StoredProcedure;
cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;cmd.ExecuteNonQuery();GbsDataReader dr = (GbsDataReader)cmd.ExecuteReader();while (dr.Read()){for (int i = 0; i < dr.FieldCount; i++){Console.WriteLine(dr[i]);}}
示例3: 有两个出参的存储过程。
create procedure call_p4() returning varchar(20),varchar(20)
begin
define v1 int;
define v2 row(col1 int,col2 varchar(20));
define rc varchar(20);
let v1 = 100;
let v2 = row(200,“testReturnValue”);
call p4(v1,v2);
return v2.col1,v2.col2;
end
end procedure;
create procedure p4(v1 int, out v2 row(col1 int, col2 varchar(20)))
begin
let v1 = 1111;
let v2 = row(2222,“P4_Return”);
end
end procedure;
两个出参的存储过程,两个出参使用的row方式。
能看出来,这是两个返回结果值。存储过程执行结果,也是两个返回值。
示例代码
GbsConnection conn = new GbsConnection(builder.ConnectionString);
conn.Open();
GbsCommand cmd = conn.CreateCommand();
mandText = “call_p4”;
mandType = mandType.StoredProcedure;
cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;cmd.ExecuteNonQuery();GbsDataReader dr = (GbsDataReader)cmd.ExecuteReader();while (dr.Read()){for (int i = 0; i <= dr.FieldCount; i++){Console.WriteLine(dr[i]);}}