in c# program before you can start using the odbc class definitions, you will need to include the...
TRANSCRIPT
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);
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();}
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
prepare the SQL we want to execute
sqlCommand.CommandText = "select * from emp order by name";
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();
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.
while(sqlReader.Read()){
Console.WriteLine(“{0} {1}”, sqlReader.GetString(0),
sqlReader.GetString(1));}
// GetName(0)
Insert
// 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()
Delete
// 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();
// 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();
Write code to insert and display employ records from Table1 in alphabetical order.
Table1 : name stringno int
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.