2

Hi I am trying to understand how to convert java code to C#, I read some tutorials about java but I dont find the way in how to translate this lines of code:

CallableStatement cstmt = con.prepareCall("?=CALL
cstmt.registerOutParameter(1, Types.JAVA_OBJECT);
cstmt.execute(); 
ResultSet rs = (ResultSet) cstmt.getObject(1); 

I try with

  CUBRIDConnection con = new CUBRIDConnection(ConnectionString);
  CUBRIDCommand com = new CUBRIDCommand();
  com.CommandType = CommandType.StoredProcedure;
  com.Connection = con;
  com.CommandText = "call mySP();";
  com.Parameters.Add("?=p", CUBRIDDataType.CCI_U_TYPE_RESULTSET).Direction =
      ParameterDirection.Output;
  con.Open();
  DbDataReader df = com.ExecuteReader(CommandBehavior.SingleResult);
  //some stuff
  con.Close();

This dont work, how can I translate it?

NewCastle79
  • 73
  • 10
  • You might translate an *algorithm* coded in Java to one coded in C#. Translating modest size programs is really, really hard because the of huge differences in the Java and C# underlying libraries that applications must use. – Ira Baxter May 11 '12 at 21:11
  • is your Java Application pointing to SQL Server or Oracle database? – Luiggi Mendoza May 11 '12 at 21:30
  • @Luiggi Mendoza: to [CUBRID](http://www.cubrid.org/) database: "**CUBRIDConnection**" – esengineer May 14 '12 at 09:01

3 Answers3

2

try this

 SqlConnection con = new SqlConnection("your connection string");
    SqlCommand com = new SqlCommand();
    com.CommandType = CommandType.StoredProcedure;
    com.Connection = con;
    com.CommandText = "yourstoredprocedurename"; //no paranthesis or call key word just procedure name
    SqlParameter parameter = new SqlParameter("yourparametername", SqlDbType.Binary); //just parameter name no questionmark
    parameter.Direction= ParameterDirection.Output;
    com.Parameters.Add(parameter);
    con.Open();
    var df = com.ExecuteReader(CommandBehavior.SingleResult);
    //some stuff
    con.Close();
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Esen
  • 973
  • 1
  • 21
  • 47
1

I have outlined the following lines which could be improved to help it work:

SqlConnection con = new SqlConnection("ValidConnectionString");    

com.CommandText = "storedProcedureName"; // You need to give the command the actual SP name, call mySP(); is invalid.

com.Parameters.Add("@ParameterName", SqlDbType.Binary).Direction = ParameterDirection.Output; // Parameters are usually prefixed with @ParameterName (some people omit the @) however this is preference amongst developers.

Note: You will have to replace "ValidConnectionString" with your connectionString (unless ConnectionString is the variable holding this information). Incase it is not, a connection string looks like: Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword; which would connect to a SQL Server 2008 database. For other connection strings, look at the following site:

http://www.connectionstrings.com/

Darren
  • 68,902
  • 24
  • 138
  • 144
0

I solve it using the following.

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

NewCastle79
  • 73
  • 10