Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2

3 In C# program

4 Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions

5  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);

6 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 stuff dbMySQL.Close(); } catch(OdbcException e) { Console.WriteLine("Database Error\n\n{0}", e.ToString()); } finally { if(dbMySQL != null) dbMySQL.Close(); }

7 After we have attained a open database connection, we need to get the command object in 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

8  prepare the SQL we want to execute sqlCommand.CommandText = "select * from emp order by name";

9 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();

10  Once we have a data reader object, we can begin to read in the row value one at a time using 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.

11 while(sqlReader.Read()) { Console.WriteLine(“{0} {1}”, sqlReader.GetString(0), sqlReader.GetString(1)); } // GetName(0)

12  Insert

13 // 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()

14  Delete

15 // 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();

16  // 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();

17 Write code to insert and display employ records from Table1 in alphabetical order. Table1 : name string no int

18  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.

19

20

21


Download ppt "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."

Similar presentations


Ads by Google