.NET     Console.WriteLine( "All Things .NET" );
.NET Nerd Blog Home
5.10.2002

 

Sample DB Code


I always seem to be searching for samples of code to quickly query a db...so here they are, straight out of my toolbox clipboard !!

OLEDB DataSet

OleDbConnection db = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=E:\databases\bulkdb.mdb;
Persist Security Info=False" );
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataSet ds = new DataSet();
adapter.SelectCommand = new OleDbCommand(
"select * from tps_login", db );

adapter.Fill( ds );


OLEDB DataReader


OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=F:\databases\bulkdb.mdb;
Persist Security Info=False" );
OleDbCommand cmd = new OleDbCommand(
"select * from TPS_HINVENTORY", conn);
conn.Open();
OleDbDataReader r = cmd.ExecuteReader(
CommandBehavior.CloseConnection);

while ( r.Read() )
{
String s = r.GetString(0);
DateTime dt = r.GetDateTime(3);
Decimal val = r.GetDecimal(4);
}


Note with the DataReader, passing CommandBehavior.CloseConnection will cause the connection to be closed whenever the user of the returned object is done with it.





Comments: Post a Comment

Powered by Blogger