如何在CUBRID数据库中使用C#调用Java存储过程

时间:2012-05-12 14:51:55

标签: c# java database cubrid

我使用以下代码来使用CUBRID数据库java存储过程。

string ConnectionString = "server=localhost;database=demodb;port=30000;user=dba;password=123456";
DataTable dt = new DataTable();
DataSet ds = new DataSet();
CUBRIDConnection con = new CUBRIDConnection(ConnectionString);
CUBRIDCommand com = new CUBRIDCommand();
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.CommandText = "select rset()";
CUBRIDParameter pan = new CUBRIDParameter();
pan.Direction = ParameterDirection.Output;
pan.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_RESULTSET;
pan.ParameterName = "?p1";
CUBRIDDataAdapter dap = new CUBRIDDataAdapter(com);
con.Open();
int val =  dap.Fill(ds);
con.Close();

并在服务器中使用服务器中的下一个函数和存储过程

public class JavaSP3 {
public static ResultSet TResultSet(){
try {
Class.forName("cubrid.jdbc.driver.CUBRIDDriver"); 
Connection con = DriverManager.getConnection("jdbc:default:connection:"); 
String sql = "select * from athlete"; 
Statement stmt=con.createStatement(); 
ResultSet rs = stmt.executeQuery(sql); 
((CUBRIDResultSet)rs).setReturnable(); 
return rs;
}
catch (Exception e) 
{
e.printStackTrace();
}
return null;        
}
}

,功能代码是这个

CREATE FUNCTION "rset"() RETURN CURSOR
AS LANGUAGE JAVA 
NAME 'JavaSP3.TResultSet() return cubrid.jdbc.driver.CUBRIDResultSet'

我使用一个导致字符串并返回值的函数运行它,但是当我更改为CUBRIDResultSet值时,不能正常工作而CUBRID说>

execute error:-911
line 1 is not executed (error)
Error description:
Invalid call: it can not return ResultSet.

拜托,我有3天时间试图解决这个问题,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

ADO.NET不提供对ResultSet的支持。

http://www.cubrid.org/?mid=forum&category=195532&document_srl=358924

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

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        string ConnectionString = "server=localhost;database=demodb;port=30000;user=dba;password=123456";
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        CUBRIDConnection con = new CUBRIDConnection(ConnectionString);

        CUBRIDCommand com = new CUBRIDCommand();
        com.CommandType = CommandType.Text; //Important ADO.NET driver crash using call convention
        com.Connection = con;
        com.CommandText = "select rset();";
        CUBRIDParameter pan = new CUBRIDParameter();
        con.Open();
        DbDataReader reader = com.ExecuteReader();
        CustomAdapter da = new CustomAdapter();
        da.FillFromReader(dt, reader);
        con.Close();

        DataRow fila = dt.Rows[0];
        Console.WriteLine(fila[0].ToString());
        Console.ReadKey();            

    }
}


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)
    {


    }
} 

java class

import java.sql.*;

import cubrid.jdbc.driver.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class JavaSP3 {
public static String TResultSet(){
ResultSet rs = null;
Statement stmt = null;
String sql;
try {
           Class.forName("cubrid.jdbc.driver.CUBRIDDriver"); 
           Connection con = DriverManager.getConnection("jdbc:default:connection:"); 
          ((CUBRIDConnection)con).setCharset("euc_kr"); 

                  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder builder =factory.newDocumentBuilder();
                  Document doc = builder.newDocument();
                  Element results = doc.createElement("Results");
                  doc.appendChild(results);

                  sql = "select * from athlete"; 
                   stmt=con.createStatement(); 
                   rs = stmt.executeQuery(sql); 
                  ResultSetMetaData rsmd = rs.getMetaData();
                  int colCount = rsmd.getColumnCount();

                 while (rs.next()) {
                    Element row = doc.createElement("Row");
                    results.appendChild(row);
                    for (int ii = 1; ii <= colCount; ii++) {
                       String columnName = rsmd.getColumnName(ii);
                       Object value = rs.getObject(ii);
                       Element node = doc.createElement(columnName);
                       node.appendChild(doc.createTextNode(value.toString()));
                       row.appendChild(node);
                    }
                  }


                 String valor = getDocumentAsXml(doc);
                 return valor;

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    return null;
}

public static String getDocumentAsXml(Document doc)
      throws TransformerConfigurationException, TransformerException {
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
    transformer.setOutputProperty
       ("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    java.io.StringWriter sw = new java.io.StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    return sw.toString();
 }

}

Cubrid功能

CREATE FUNCTION "rset"() RETURN STRING
AS LANGUAGE JAVA 
NAME 'JavaSP3.TResultSet() return java.lang.String'

这允许您使用ADO.NET

从CUBRID中的Java存储过程获取数据