WPF DEV gridcontrol当前项的数据导出为mdb文件

 

     /// <summary>
        /// 导出为mdb
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExportMdb_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (DataTable dts = new DataTable())
                {
                    #region 查找数据源
                    foreach (DevExpress.Data.DataColumnInfo item in user_GridControl.DataController.Columns)
                        dts.Columns.Add(item.Name.ToString().Trim().ToLower(), item.Type.Name.GetType());
                    foreach (System.Data.DataRowView item0 in user_GridControl.DataController.GetAllFilteredAndSortedRows())
                    {
                        DataRow row = dts.NewRow();
                        foreach (DataColumn item in dts.Columns)
                            row[item.ColumnName] = item0.Row[item.ColumnName];
                        dts.Rows.Add(row);
                    }
                    #endregion
                    #region 0.用户保存文件的路径
                    string paths = string.Empty;
                    Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
                    sfd.Filter = "mdb|*.mdb";
                    if (sfd.ShowDialog() == true)
                    {
                        #region 1.创建mdb文件(创建数据库)
                        paths = sfd.FileName;
                        if (string.IsNullOrWhiteSpace(paths))
                            return;
                        if (!CreateAccessDb(paths))
                            System.Windows.MessageBox.Show("创建表失败...");
                        //MessageBox.Show("创建成功");
                        #endregion
                    }
                    #endregion
                    #region 2.创建表 
                    string Table = btn_text.Replace("(", "").Replace(")", "");
                    List<string> strList = new List<string>();
                    StringBuilder ziduan = new StringBuilder();
                    foreach (DataColumn item in dts.Columns)
                    {
                        ziduan.Append("," + item.ColumnName + " text");
                    }
                    strList.Add(string.Format("create table {0}({1})", Table, ziduan.ToString().Substring(1)));
                    MDBHelper.CreateCYTable(MDBHelper.CreateConnectionNew(paths), strList);
                    #endregion
                    #region 3.添加数据
                    StringBuilder Tableziduan = new StringBuilder();
                    foreach (DataColumn item in dts.Columns)
                        Tableziduan.Append("," + item.ColumnName);

                    List<string> insertSQLs = new List<string>();
                    int k = 0;
                    foreach (System.Data.DataRowView item0 in user_GridControl.DataController.GetAllFilteredAndSortedRows())
                    {
                        k++;
                        StringBuilder data = new StringBuilder();
                        foreach (DataColumn item in dts.Columns)
                            data.Append(",'" + ((item0.Row[item.ColumnName].ToString() == "" || item0.Row[item.ColumnName].ToString() == null) ? "" : item0.Row[item.ColumnName].ToString()) + "'");
                        insertSQLs.Add(string.Format("INSERT INTO {0}({1}) VALUES({2});", Table, Tableziduan.ToString().Substring(1), data.ToString().Substring(1)));
                        if (k == 5000)
                        {
                            int ExecuteSuccessfully0 = MDBHelper.ExcuteListSQL(insertSQLs, paths);
                            k = 0;
                            insertSQLs.Clear();
                        }
                    }
                    int ExecuteSuccessfully1 = MDBHelper.ExcuteListSQL(insertSQLs, paths);
                    #endregion 
                }
                MessageBox.Show("导出成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

 

posted @ 2019-03-21 10:10  紫晶城  阅读(378)  评论(0编辑  收藏  举报