[Turn] corresponding to the difference between Java JDBC database of C # ADO connection

JDBC database corresponding to the difference between the connector C #

Before been using java, recently found a job as .NET, I began to learn .NET.
Today is also checked a lot of information, but has not seen Bowen and contrast between JDBC, start is confused!
Hard work pays off eventually but probably understand the C # how to connect to the database, and it is the difference between JDBC, is like me a little help sprout new simple analysis!

A rookie, talking about the basis for comparison, the god skip it!

The following topic
preparations
JDBC in this step should be introduced into the corresponding jar package, C # introduced corresponding DLL (these things Web search will be there, another eclipse using maven, vs using nuget can also be found in the corresponding resource, very convenient! not repeat them here)

to mysql for example
load the driver
JDBC first, load the driver:

The Class.forName ( " com.mysql.jdbc.Driver " );   // drive parameters provided by database corresponding

 C # DLL is introduced directly into the project, there should be no corresponding step. (Correspondence if you really want to do is using MySql.Data.MySqlClient ???)

Obtaining a connection
JDBC here should begin to obtain a database connection:

Connection connection = DriverManager.getConnection(url, username, password);
/**
*Parameter Description:
* Url: address to be connected to a database similar to jdbc: mysql: // localhost: 3306 / test can also be added directly to the url method getConnection after the user name and password
* Username: username to log database needed
* Password: username password corresponding
*/

 ADO is also here to get database connection

MySqlConnection connection = new MySqlConnection(url);
/**
*Parameter Description:
* Url: the address of the database to be connected. Similarly Server = localhost; Database = test; username = root; Password = root;
* MySqlConnection only because no constructor parameters and a parameter, so he can only pass with the username and password.
*/

 Gets statement and the Command
JDBC in this step is to obtain the corresponding Statement and PreparedStatement:

Statement statement = connection.createStatement();
or
PStatement PreparedStatement = Connection.prepareStatement (sql); // sql sql statement is to be executed

  ADO same, but it does not seem to participate and there is no way to distinguish the sql:

The Command = the MySqlCommand connection.CreateCommand ();  
 // Maybe I did not find the sql of class distinction, but there are no arguments and parameters are normal sql executed, this will be discussed below

 Sql parameter settings

sql statement with parameters in JDBC and C # is not the same

select * from user where id = ? ;   //这是jdbc

select * from user where id = @id;  //这是C#

 JDBC:

    pStatement.setString (index, value); // index corresponds? Position, starting at 1


    // execute SQL 
    int RES = Statement.executeUpdate (SQL); // deletions to return the number of rows affected 
    or
    The resultSet the ResultSet = Statement.executeQuery (SQL); // this is to return a result set

 ADO:

command.CommandText = sql;
 command.Parameters.AddWithValue(parameterName, value);
 // parameterName corresponds to the name behind the @ sql, value corresponding value. In no particular order (of course there are other ways to set parameters, this is more intuitive, and the rest is not to say, we all look on the line)

// perform 
Connection.Open ();
 int RES = command.ExecuteNonQuery (); // perform additions and deletions to the corresponding statement that returns the number of rows affected 
the MySqlDataReader = Command.ExecuteReader Reader (); // execute the query sql statement, returns an Object DbDataReader

 Traverse the result set
and then traverse the result is
JDBC:

while(resultSet.next()){
            ID String = resultSet.getString ( 1 );    // Get the results of the index, starting from 1 
            String name = resultSet.getString ( " name " ); // get the result according to the column name 
}

 ADO :

 while (reader.Read())
    {
         Object ID = Reader [ 0 ]; // Get the index, starting with zero 
         Object name = Reader [ " name " ]; // obtained according to the column name

or
        /*
         * string id = reader.getString(0);
         * string name = reader.getString("name");
        */                  
    }

 
Close the connection
end are required to release related resources !!! (very important)

and finally the release of all relevant resources required !!! (very important)

and finally the release of all relevant resources required !!! (very important)

转自CSDN: https://blog.csdn.net/qingming7573/article/details/53143180

Guess you like

Origin www.cnblogs.com/shuai7boy/p/11783375.html