Open Source RDBMS - Seamless, Scalable, Stable and Free

English | Login |Register


1
(click on this box to dismiss)

How to call Java Stored Procedure from C#? (Solved)

Hi, I am trying to get some value from the database using ado.net.

I am using the java stored procedures but in the manager this works, in my C# code not.

public class SpCubrid{
  public static String HelloCubrid() {
    return "Hello, Cubrid !!";
  }
  public static int SpInt(int i) {
    return i + 1;
  }
  public static void outTest(String[] o) {
    o[0] = "Hello, CUBRID";
  }
}

CREATE FUNCTION "hello"() RETURN STRING
AS LANGUAGE JAVA 
NAME 'SpCubrid.HelloCubrid() return java.lang.String'

result on server:

Hello, Cubrid !!

my C# code

using System;
using System.Data;
using System.Windows.Forms;
using CUBRID.Data.CUBRIDClient;
using System.Data.Common;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string ConnectionString = "server=localhost;database=demodb;port=30000;user=dba;password=123456";
            DataTable dt = new DataTable();
           
            CUBRIDConnection con = new CUBRIDConnection(ConnectionString);

            CUBRIDCommand com = new CUBRIDCommand("hello", con);
            com.CommandType = CommandType.StoredProcedure;            
           
            con.Open();
            DbDataReader reader = com.ExecuteReader();
            
            CustomAdapter da = new CustomAdapter();
            da.FillFromReader(dt, reader); 
            con.Close();
            string text = dt.Rows[0].ToString();
            //dataGridView1.DataSource = dt;
            //MessageBox.Show(reader.ToString());
           
        }
    }

    public class CustomAdapter : System.Data.Common.DbDataAdapter
    {
        public int FillFromReader(DataTable dataTable, IDataReader dataReader)
        {
            return this.Fill(dataTable, dataReader);
        }
        protected override System.Data.Common.RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow a, IDbCommand b, StatementType c, System.Data.Common.DataTableMapping d)
        {
            return (System.Data.Common.RowUpdatedEventArgs)new EventArgs();
        }

        protected override System.Data.Common.RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow a, IDbCommand b, StatementType c, System.Data.Common.DataTableMapping d)
        {
            return (System.Data.Common.RowUpdatingEventArgs)new EventArgs();
        }

        protected override void OnRowUpdated(System.Data.Common.RowUpdatedEventArgs value)
        {

        }
        protected override void OnRowUpdating(System.Data.Common.RowUpdatingEventArgs value)
        {

        }
    } 
}
링크 댓글 쓰기 (0) 선택된 답변
질문시간 2년 전
castle79
60
11 답변들
1

Only need to use select hello() instead call hello()

com.CommandType = CommandType.Text;
            com.Connection = con;
            com.CommandText = "select hello();";

링크 댓글 쓰기 (1)
답변시간 2년 전
castle79
60




You are either using a very old browser or a browser that is not supported.
In order to browse cubrid.org you need to have one of the following browsers:



Internet Explorer: Mozilla Firefox: Google Chrome: