zoukankan      html  css  js  c++  java
  • 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);
                }
            }
  • 相关阅读:
    mysql distinct 去重
    基于visual Studio2013解决面试题之1004最长等差数列
    基于visual Studio2013解决面试题之1003字符串逆序
    基于visual Studio2013解决面试题之1002公共子串
    基于visual Studio2013解决面试题之1001去除数字
    基于visual Studio2013解决面试题之0909移动星号
    基于visual Studio2013解决面试题之0908最大连续数字串
    基于visual Studio2013解决面试题之0907大数乘法
    基于visual Studio2013解决面试题之0905子串数量
    基于visual Studio2013解决面试题之0902内存拷贝
  • 原文地址:https://www.cnblogs.com/SeNaiTes/p/10569836.html
Copyright © 2011-2022 走看看