C#:Read From a Database

From Progzoo
Revision as of 07:40, 19 June 2008 by 146.176.162.21 (talk)
Jump to: navigation, search

To read from a SQL Server database you can use the classes in System.Data.SqlClient

  • SqlCommand
  • SqlConnection
  • SqlDataReader
Connection parameters
valuedescription
127.0.0.1 the IP address database server;
gisq the name of the database;
scott user name;
tiger the password;

Read the name and population for each country with a population of more than 200000000.

The SQL statement is:
SELECT name, population
FROM bbc
WHERE population>200000000

The table bbc has columns:

  • name
  • region
  • area
  • population
  • gdp


[Font] [Default] [Show] [Resize] [History] [Profile]

To read from a non SQL Server database you can use the classes in System.Data.OdbcClient

  • OdbcCommand
  • OdbcConnection
  • OdbcDataReader

You will need to set up an Odbc connection first. In this exampple I use an ODBC connection called MySQLscott. I had to download the mysql-odbc-connector from mysql.com first. I had to create the ODBC connection from Control Panel/Administrative Tools/Datasoutrces (ODBC)

Connection parameters
valuedescription
127.0.0.1 the IP address database server;
gisq the name of the database;
scott user name;
tiger the password;

Read the name and population for each country with a population of more than 200000000.

The SQL statement is:
SELECT name, population
FROM bbc
WHERE population>200000000

The table bbc has columns:

  • name
  • region
  • area
  • population
  • gdp


[Font] [Default] [Show] [Resize] [History] [Profile]