欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

oledb连接access数据库示例

程序员文章站 2024-02-18 22:40:46
复制代码 代码如下:using system;using system.collections.generic;using system.text;usin...

复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.data;
using system.data.oledb;

namespace consoleapplication1
{
    class program
    {
        static void main(string[] args)
        {
            oledbconnection conn = new oledbconnection();
            oledbcommand comm = new oledbcommand();
            oledbdatareader dr=null;
            conn.connectionstring = "provider=microsoft.jet.oledb.4.0;data source=" + system.environment.currentdirectory + "\\simplenews.mdb";
            comm.connection = conn;
            comm.commandtext = "select * from [login]";
            try
            {
                conn.open();
                dr = comm.executereader();
                while (dr.read())
                {
                    for (int i = 0; i < dr.fieldcount; i++)
                        console.write(dr.getvalue(i) + "  ");
                    console.writeline("");
                    //dr.close();
                }
                dr.close();
            }
            catch (exception err)
            {
                console.writeline(err.message);
            }
            finally
            {
                comm = null;
                conn.close();
                comm = null;
                console.readkey();
            }
        }
    }
}