//  最简单的C#操作数据库程序


using System;
using System.Data.OleDb;
class OleDbTest
{
    public static void Main()
    {
        //创建数据库连接
        OleDbConnection aConnection = new OleDbConnection(
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=wms.mdb");
        //创建command对象并保存sql查询语句
        OleDbCommand aCommand = new OleDbCommand("select * from IN_PAGE",
            aConnection);
        try
        {
            aConnection.Open();
            //创建datareader 对象来连接到表单
            OleDbDataReader aReader = aCommand.ExecuteReader();
            Console.WriteLine("This is the returned data from IN_PAGE table");
            //Console.WriteLine(aReader.GetInt32(0).ToString());
            //循环遍历数据库
            while (aReader.Read())
            {
                // And iterate through the data
                Console.WriteLine("{0,-30} {1,30},{2,30}", aReader[0],
                    aReader[1], aReader[2]);
                // Console.WriteLine(aReader.GetInt32(0).ToString());
            }
            //关闭reader对象
            aReader.Close();
            //关闭连接,这很重要
            aConnection.Close();
        }
        //一些通常的异常处理
        catch (OleDbException e)
        {
            Console.WriteLine("Error: {0}", e.Errors[0].Message);
        }
    }
} 

  

posted on 2015-11-13 15:34  林,天宇  阅读(532)  评论(0编辑  收藏  举报