Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys I need your help,
So the code below is simple, it query's a database table(User) for everything in it and puts that data in a result set. I wanted only the firstname from the table, but I commented it out for now since it works.
Java
public static void main(String[] args) throws Exception
{
    // TODO Auto-generated method stub

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://rideon.db.12096379.hostedresource.com:3306/rideon","rideon","Rideon12#");

    PreparedStatement statement = con.prepareStatement("select * from User");

    //ResultSet result = statement.executeQuery();

    ResultSet result  = statement.executeQuery();

    closeResultSet(result);

    while(result.next())
    {

    //System.out.println(result.getS­tring(1) + " " + result.getString(2));
        //System.out.println(result.getString("FirstName"));


    }

 }


now this second code checks if the passed result set is not empty. If its not, then a second statement object is created and the first statement object that populated the result set is passed to it and closed

Java
 public static void closeResultSet(ResultSet rs)
 {
      try {
            if (rs != null)
            {
               Statement st = rs.getStatement();

               System.out.println("This is what is in the statement:"+ st);

                   st.close();
            }
    }catch (Exception e)
    {
        System.out.println("spider monkey");
    }
}


if I go and print out what is in the second statement, this is what I get

Printout: This is what is in the statement:com.mysql.jdbc.JDBC4PreparedStatement@1a4788f3: select * from User


This is what I passed to Prepared statement originally.

So my question is, is there a way I can mirror The second function(closeResultSet(ResultSet rs))in C#.


Thanks for your help.
Posted
Updated 22-Jan-14 12:43pm
v2

1 solution

Of course you can do it in C# - there are plenty of options for working with MySQL

The question is, do you just want to parrot exactly what you have in java ? (least likely) - or just rewrite the 'functionality' in C# .

When it all boils down, you have

a connection to MySQL
One or more Queries/DB Operations
Fetching Results

None of these appear difficult in MySQL Connector/Net (and Ive been studying MySQL recently because I need to use it for a project myself)

I'd have a look at this Connect C# to MySQL[^] to start with

The MySQL site has plenty of documentation/examples for C# using Connector/Net btw

'g'

[edit] I havnt mentioned anything about 'direct SQL' vs a DAL and Stored Procs, that's really up to you to determine direction on - plusses an minusses [/edit]
 
Share this answer
 
v2
Comments
rudolph098 22-Jan-14 18:42pm    
Sorry for any misunderstanding, I perform the first part of the code in C#. Its performing the second part of the code in C# that has me hooked. And also I am using (using System.Data.SqlClient;) which is .Net built in sqlconnection framework.
Garth J Lancaster 22-Jan-14 18:51pm    
well, in that case, Im pretty sure MySQL has an ODBC driver that you can use with System.Data.SqlClient

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900