更新日期:2024年09月11日
使用GBase
ADO.NET 调用一个存储过程,要先创建一个GBaseCommand 对象
并且使用属性CommandText 传递存储过程名,并将属性CommandType 设为
CommandType.StoredProcedure。为存储过程中的每个输入、输出参数创建一个
GBase 8a 程序员手册ADO.NET 篇
- 24 -
南大通用数据技术股份有限公司
GBaseCommand 参数。
使用GBaseCommand.ExecuteNonQuery()方法来调用存储过
程。
调用存储过程的步骤:
1) 使用GBaseConnection 创建数据库连接对象
2) 使用GBaseCommand 创建命令对象
3) 使用连接对象打开连接
4) 设置命令对象的属性CommandType 为CommandType.StoredProcedure,
并关联连接对象
5) 设置命令对象的参数与库中要访问的存储过程参数一一对应
6) 执行命令对象的ExecuteNonQuery 方法完成调用
7) 使用 GBaseCommand.Parameters 的属性 Value 获得输出参数的值
8) 关闭数据库连接
下面是使用GBase ADO.NET 调用一个存储过程并获取输出参数的完整例
子。
C# 示例:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GBase.Data;
using GBase.Data.GBaseClient;
using System.Diagnostics;
namespace UsingStoredRoutines
{
class Program
{
GBase 8a 程序员手册ADO.NET 篇
南大通用数据技术股份有限公司
- 25 -
static void Main(string[] args)
{
GBaseConnection _Conn = new GBaseConnection();
_Conn.ConnectionString =
"server=192.168.5.41;user=root;database=test;password=1;pooling=fals
e";
GBaseCommand _Cmd = new GBaseCommand();
try
{
Console.WriteLine("Connecting to GBase...");
_Conn.Open();
_Cmd.Connection = _Conn;
_Cmd.CommandText = "add_emp";
_Cmd.CommandType = CommandType.StoredProcedure;
_Cmd.Parameters.AddWithValue("@lname", "Jones");
_Cmd.Parameters["@lname"].Direction =
ParameterDirection.Input;
_Cmd.Parameters.AddWithValue("@fname", "Tom");
_Cmd.Parameters["@fname"].Direction =
ParameterDirection.Input;
_Cmd.Parameters.AddWithValue("@bday",
"1940-06-07");
_Cmd.Parameters["@bday"].Direction =
ParameterDirection.Input;
_Cmd.Parameters.AddWithValue("@empno",
GBaseDbType.Int32);
_Cmd.Parameters["@empno"].Direction =
ParameterDirection.Output;
_Cmd.ExecuteNonQuery();
Console.WriteLine("Employee number: " +
GBase 8a 程序员手册ADO.NET 篇
- 26 -
南大通用数据技术股份有限公司
_Cmd.Parameters["@empno"].Value);
Console.WriteLine("Birthday: " +
_Cmd.Parameters["@bday"].Value);
}
catch (GBaseException ex)
{
Console.WriteLine("Error " + ex.Number + " has
occurred: " + ex.Message);
}
finally
{
if (_Conn != null)
_Conn.Close();
Console.WriteLine("None.");
}
}
}
}
sword GCIParamGet(
CONST dvoid *hndlp,
ub4 htype,
GCIError *errhp,
dvoid **parmdpp,
ub4 pos
);
返回描述句柄或语句句柄中的根据位置指定的参数描述符。
GBase 8s GCI 接口使用指南
南大通用数据技术股份有限公司 44
参数
输入
/输出
hndlp
输入
一个存放描述信息的句柄
htype
输入
hndlp参数句柄的类型,支持以下两种类型:
GCI_HTYPE_STMT – 语句句柄
GCI_DTYPE_PARAM – 参数句柄
errhp
输入
错误信息句柄, 该接口调用失败时, 错误信息会存在该句柄上
parmdpp
输出
输出的描述符句柄, 通过该句柄调用GCIAttrGet函数就可以得到指
定位置上的描述信息
pos
输入
要获取描述符上指定位置的描述信息
如果执行成功, 返回GCI_SUCCESS,否则返回GCI_ERROR。