in c# program before you can start using the odbc class definitions, you will need to include the...

21

Upload: dwayne-carpenter

Post on 15-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions
Page 2: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions
Page 3: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

In C# program

Page 4: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

Before you can start using the ODBC class definitions, you will need to include the

right module.

using System.Data.Odbc; // ODBC definitions

Page 5: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

A Connection is made using the OdbcConnection class and passing a connection string to the object being created.

string strConnect = "DSN=MySQL;UID=root;PWD=admin;DATABASE=store";

OdbcConnection dbMySQL = new OdbcConnection(strConnect);

Page 6: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

using System.Data.Odbc; // ODBC definitions

class Program{static void Main(string[] args){string strConnect =

"DSN=MySQL;UID=root;PWD=admin;DATABASE=test";OdbcConnection dbMySQL = new

OdbcConnection(strConnect);try{dbMySQL.Open();// do some database stuffdbMySQL.Close();}catch(OdbcException e){Console.WriteLine("Database Error\n\n{0}", e.ToString());}finally{if(dbMySQL != null) dbMySQL.Close();}

Page 7: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

After we have attained a open database connection, we need to get the command objectin order to execute our SQL.

OdbcCommand sqlCommand = dbMySQL.CreateCommand();

A SqlCommand object allows you to specify what type of interaction you want to perform with a database

Page 8: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

prepare the SQL we want to execute

sqlCommand.CommandText = "select * from emp order by name";

Page 9: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

Since this SQL statement will be returning a result table we need to call the ExecuteReader( ) method on the OdbcCommand object "sqlCommand".

OdbcDataReader sqlReader = sql.ExecuteReader();

Page 10: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

Once we have a data reader object, we can begin to read in the row value one at a timeusing the Read( ) method.

This method will return 'true' if there are more rows to be fetched, and 'false' when there are no more rows existing in the result table.

Page 11: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

while(sqlReader.Read()){

Console.WriteLine(“{0} {1}”, sqlReader.GetString(0),

sqlReader.GetString(1));}

// GetName(0)

Page 12: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

Insert

Page 13: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

// prepare command string string insertString = @"     insert into Categories     (CategoryName, Description)     values ('Miscellaneous', 'Whatever doesn’t fit elsewhere')";  // 1. Instantiate a new command with a query and connection SqlCommand cmd = new SqlCommand(insertString, conn);  // 2. Call ExecuteNonQuery to send command cmd.ExecuteNonQuery()

Page 14: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

Delete

Page 15: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

// prepare command string string deleteString = @"     delete from Categories     where CategoryName = 'Other'";  // 1. Instantiate a new command SqlCommand cmd = new SqlCommand();  // 2. Set the CommandText property cmd.CommandText = deleteString;  // 3. Set the Connection property cmd.Connection = conn;  // 4. Call ExecuteNonQuery to send command cmd.ExecuteNonQuery();

Page 16: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

// 1. Instantiate a new command SqlCommand cmd = new SqlCommand("select count(*) from Categories", conn);  // 2. Call ExecuteNonQuery to send command int count = (int)cmd.ExecuteScalar();

Page 17: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

Write code to insert and display employ records from Table1 in alphabetical order.

Table1 : name stringno int

Page 18: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

A SqlCommand object allows you to query and send commands to a database. 

It has methods that are specialized for different commands. 

The ExecuteReader method returns a SqlDataReader object for viewing the results of a select query. 

For insert, update, and delete SQL commands, you use the ExecuteNonQuery method. 

If you only need a single aggregate value from a query, the ExecuteScalar is the best choice.

Page 19: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions
Page 20: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions
Page 21: In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions