存储过程与函数
达梦数据库.NET Data Provider 可以使用 DmCommand 对象来执行存储过程与函数。
创建一个存储过程“UPDATEPRODUCT”用来更新表 PRODUCT 中的 NAME 字段,存储过程
代码如下:
CREATE OR REPLACE PROCEDURE "PRODUCTION"."UPDATEPRODUCT" ( V_ID INT, V_NAME VARCHAR(50) ) AS BEGIN UPDATE PRODUCTION.PRODUCT SET NAME = V_NAME WHERE PRODUCTID = V_ID; END; 达梦数据库.NET Data Provider 调用存储过程的示例代码如下: using System; using System.Collections.Generic; using System.Text; using Dm; namespace DMDemo { class ProcDemo { //返回结果 static int ret = 1; static DmConnection cnn = new DmConnection(); [STAThread] static int Main(string[] args) { try { cnn.ConnectionString = "Server=localhost; User Id=SYSDBA; PWD=SYSDBA"; cnn.Open(); ProcDemo demo = new ProcDemo(); demo.TestFunc(); cnn.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); return ret; } public void TestFunc() { DmCommand command = new DmCommand(); command.Connection = cnn; try { command.CommandText = "PRODUCTION.UPDATEPRODUCT"; command.CommandType = System.Data.CommandType.StoredProcedure; DmParameter parm1 = command.Parameters.Add(":V_ID", DmDbType.Int32); parm1.Value = 1; parm1.Direction = System.Data.ParameterDirection.Input; DmParameter parm2 = command.Parameters.Add(":V_NAME", DmDbType.VarChar); parm2.Value = "红楼梦(下)"; parm2.Direction = System.Data.ParameterDirection.Input; command.ExecuteNonQuery(); string a, b, c; command.Parameters.Clear(); command.CommandText = "SELECT NAME, AUTHOR, PUBLISHER FROM PRODUCTION.PRODUCT;"; DmDataReader reader = command.ExecuteReader(); while (reader.Read()) { a = reader.GetString(0); b = reader.GetString(1); c = reader.GetString(2); Console.WriteLine("NAME:" + a); Console.WriteLine("AUTHOR:" + b); Console.WriteLine("PUBLISHER:" + c); Console.WriteLine("-------------------"); } } catch (Exception ex) { Console.WriteLine(ex.Message); ret = 0; } } } }
以上代码调用存储过程“UPDATEPRODUCT” 将 PRODUCT 表中的 PRODUCTID 字 段内容为 1 的记录的 NAME 字段内容由"红楼梦"更新为"红楼梦(上)"。
https://xpanx.com/
评论