Thursday, April 24, 2008

Calling a Packaged Procedure via .Net

Someone recently posted a question in the Postgres Forums about how to call a package in Advanced Server. He also had a question about how to call a packaged procedure in a specific schema.

Here is my reply to the question:

You can't actually execute packages. You execute the procedures or functions in them. Here is some code to execute a procedure in a package. I am using public as the schema name. If you want to execute a procedure in a user's schema, just replace public with the user's name.

using System;
using System.Data;
using EnterpriseDB.EDBClient;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
EDBConnection conn =
new EDBConnection(
"Server=localhost;" +
"Port=5444;" +
"User Id=enterprisedb;" +
"Password=enterprisedb;" +
"Database=edb");

conn.Open();
EDBCommand cmd = new EDBCommand();
cmd.Connection = conn;

int vEmpId = 7876;
String query = "public.emp_admin.fire_emp(:e_id)";
cmd.CommandText = query;
cmd.CommandType = CommandType.StoredProcedure;

EDBParameter prm = new EDBParameter(
"e_id",
EDBTypes.EDBDbType.Integer);

cmd.Parameters.Add(prm);

cmd.Parameters[0].Value = vEmpId;

cmd.Prepare();

cmd.ExecuteNonQuery();

conn.Close();
}
}
}


Software Blogs - Blog Catalog Blog Directory Software blogs Top Blog Sites Blog Flux Directory Lewis Cunningham