• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Convert this simple Java prog to C# : I'm trying to learn.

Hello experts,
I've recently become very interested in C#.
I purchased a few books on the subject and plan to learn it.

Anyhow, what's a better way to learn then by example and pracitce?

I would be very greatful if someone could convert the following Java program to a C# program.

*******CODE STARTS HERE*************

package woj.mysql;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * @author Wojciech Dunski
 *
 */
public class reloadMaster {
      
      private ResultSet rs;
      private static Connection conn;
    private Statement stat;
   
    public reloadMaster(){
          try{
                conn = getConnection();
          }
          catch (SQLException e)
        {  
           e.printStackTrace();
        }
          catch(IOException ioe){
                ioe.printStackTrace();
          }
    }
   
    public static Connection getConnection()
          throws SQLException, IOException
          {
          System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/inventory";
        String username = "root";
        String password = "sunrise";
        return DriverManager.getConnection(url, username, password);
          }
   
    public void executeQueries(){
          boolean done;
          try{             
                String query = "DELETE masterInventory from masterInventory;";
                Statement st = conn.createStatement();
            done = st.execute(query);
           
             Statement stt = conn.createStatement();
             String reload = "LOAD DATA local INFILE 'C:/masterin.csv' " +
                         "INTO TABLE masterinventory FIELDS TERMINATED BY ',' " +
                         "OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n';";
             done = stt.execute(reload);
           
              }
              catch (SQLException e)
            {  
               e.printStackTrace();
            }
    }
      /**
       * @param args
       */
      public static void main(String[] args) {
            reloadMaster reload = new reloadMaster();
            reload.executeQueries();
      }
}

***** CODE ENDS HERE ********

I would love it if someone could convert this to C# and point out some of the major differences.
C# seems very interesting : )

Thanks very much!
Wojciech Dunski.
0
wdunski
Asked:
wdunski
  • 2
1 Solution
 
mmarksburyCommented:
Better yet, go to http://www.asp.net/vwd/starterkits.aspx?tabIndex=4&tabId=46 and download some of the starter kits.  This is how I learned, and it was amazing at how easy it is to pick up.  I've just made the move from C# to Java due to a work move, and all I can say is you'll be much happier with C#.
0
 
findsukeshCommented:
Hi Wojciech,

The corresponding C# code would for accessing MYSQL Database would like as below

------------------------------------------------------
public class reloadMaster
      {
     
            private static OdbcConnection conn;
            
            public reloadMaster()
            {
                  try
                  {
                        conn = getConnection();
                  }
                  catch (OdbcException odbce)
                  {  
                        Console.WriteLine(odbce.StackTrace);
                  }
                  catch(Exception e)
                  {
                        Console.WriteLine(e.StackTrace);
                  }
            }
            public static OdbcConnection getConnection()
            {
                  String strconn = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=inventory;uid=root;pwd=sunrise;option=3";
                  return DriverManager.getConnection(strconn);
                  /*
                   * In the DriverManager you will have to write code like below
                   * to create the connection object
                   *
                   * OdbcConnection odbcconn = new OdbcConnection(strconn);
                   *  
                   */
            }

            public void executeQueries()
            {
                  try
                  {            
                        String query = "DELETE masterInventory from masterInventory;";
                        OdbcCommand odbccmd = conn.CreateCommand();
                        odbccmd.CommandText = query;
                        odbccmd.ExecuteNonQuery();
           
                        OdbcCommand odbccmd2 = conn.CreateCommand();
                        String reload = "LOAD DATA local INFILE 'C:/masterin.csv' " +
                              "INTO TABLE masterinventory FIELDS TERMINATED BY ',' " +
                              "OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n';";
                        odbccmd2.CommandText = reload;
                        odbccmd2.ExecuteNonQuery();
           
                  }
                  catch (OdbcException e)
                  {  
                        Console.WriteLine(e.StackTrace);
                  }
            }
            public static void main(String[] args)
            {
                  reloadMaster reload = new reloadMaster();
                  reload.executeQueries();
            }

    }
-------------------------------------------------------

But as suggested by other experts, It is better to go through the documentation, starter kits and other samples available at microsoft msdn site http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/netfxanchor.asp

Thanks and Regards,
Sukesh
0
 
wdunskiAuthor Commented:
Sounds good.
One last question:
Which namespace do I need to use for the OdbcConnection?
I treid System.Data.Obdc but that doesnt seem to work.

Thanks.
0
 
findsukeshCommented:
Hi Wojciech,

You have to use the namespace System.Data.Odbc. Please check the spelling you have used.

Try and use Visual Studio intellisense to avoid such spelling mistakes.

Regards,
Sukesh.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now