2012-05-12 43 views
0

我正在使用以下代碼來使用CUBRID數據庫java存儲過程。如何在CUBRID數據庫中用C#調用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

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類

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

得到的Java存儲過程CUBRID數據
相關問題