Winform读取Access数据库,并取数据装入DataSet

来源:互联网 发布:90后网络女红人 编辑:程序博客网 时间:2024/05/21 07:48
 public static DataSet ReadAllData()
        {
            string mdbPath = System.Configuration.ConfigurationSettings.AppSettings["fileMdb"];
            //string table = System.Configuration.ConfigurationSettings.AppSettings["table"];
            //String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + mdbPath + "'";
            DataSet dset = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                DataRow dr;
                //1、建立连接 Microsoft.Jet.OLEDB.4.0
                string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=123456";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接 
                odcConnection.Open();
                //建立SQL查询 
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句 
                odCommand.CommandText = "select * from test";


                OleDbDataAdapter ODbda = new OleDbDataAdapter();
                ODbda.SelectCommand = odCommand;
                ODbda.Fill(dset, "caiwu");
                odcConnection.Close();




            }
            catch (Exception e)
            {
                throw e;
            }


            return dset;
        }
0 1