Created
April 4, 2015 10:16
-
-
Save kamawanu/e7743591fddb84c8f195 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data; | |
using System.Data.Odbc; | |
public class Test | |
{ | |
public static void Main(string[] args) | |
{ | |
// have an ODBC DSN setup named MYSQLDSN | |
// that accesses a MySQL database via | |
// MyODBC driver for ODBC with a | |
// hostname of localhost and database test | |
string connectionString = | |
"DSN=MYSQLDSN;" + | |
"UID=myuserid;" + | |
"PWD=mypassword"; | |
IDbConnection dbcon; | |
dbcon = new OdbcConnection(connectionString); | |
dbcon.Open(); | |
IDbCommand dbcmd = dbcon.CreateCommand(); | |
// requires a table to be created named employee | |
// with columns firstname and lastname | |
// such as, | |
// CREATE TABLE employee ( | |
// firstname varchar(32), | |
// lastname varchar(32)); | |
string sql = | |
"SELECT firstname, lastname " + | |
"FROM employee"; | |
dbcmd.CommandText = sql; | |
IDataReader reader = dbcmd.ExecuteReader(); | |
while(reader.Read()) { | |
string FirstName = (string) reader["firstname"]; | |
string LastName = (string) reader["lastname"]; | |
Console.WriteLine("Name: " + | |
FirstName + " " + LastName); | |
} | |
// clean up | |
reader.Close(); | |
reader = null; | |
dbcmd.Dispose(); | |
dbcmd = null; | |
dbcon.Close(); | |
dbcon = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment