ASP.NET - Connection with OleDb

本文详细介绍了使用ADO.NET进行数据库访问的基本步骤,包括导入命名空间、创建连接、定义SQL命令、创建数据适配器及填充数据集等过程。

step1. Import two namespace "System.Data" and "System.Data.OleDb" or "System.Data.SqlConnection"
    <%@ import namespace="System.Data" %>
    <%@ import namespace="System.Data.OleDb (or System.Data.SqlConnection)" %>

step2. Creates a connection. Define two variables with the type of string, named ConnectionString and CommandText. To hold the connection string and the text for the command to run. In the ConnectionString always use salsh character(\\) instand of the slash character(\), or use the @ symbol before part of the string.
    string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                        @"Data Source=[Driver]:\......\*.mdb";
    System.Data.IDbConnection dbConnection = 
        new System.Data.OleDb.OleDbConnection(connectionString);

step3. Creates a command. first,define a SQL string:
    string queryString = "...The SQL Command...";
Now define the command object:
    System.Data.IDbCommand dbCommand = new System.Data.OleDbCommand();
    dbCommand.CommandText = queryString;
    dbCommand.Connection = dbConnection;

step4. Creates a data adapter. 
    System.Data.IDbDataAdapter dataAdapter = 
        new System.Data.OleDb.OleDbDataAdapter();
The  data adapter is the link between the page and the data, it provides not only data fetching, but also data modification. There are four objects we could use: SelectCommand(Fetches data), UpdataCommand, InsertCommand, DeleteCommand.
    dataAdapter.[object] = dbCommand;

step5. Fetch the data. Use the Fill() method of the data adapter, passing in the DataSet. This opens the database connection, runs the command, place the data into the DataSet,and then closes the database connection.
    System.Data.DataSet = new System.Data.DataSet();
    dataAdapter.Fill(dataSet);
    return dataSet
;

转载于:https://www.cnblogs.com/lory/archive/2006/08/12/475308.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值