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

Posted 紫晶城

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
            }
        }

 

以上是关于WPF DEV gridcontrol当前项的数据导出为mdb文件的主要内容,如果未能解决你的问题,请参考以下文章

WPF 修改dev GridControl行背景色

WPF GridControl控件的用法举例

C# WPF GridControl用法举例

WPF问题,richtextbox控件能绑定到数据表中的一个字段吗?怎么做? listbox选中当前项的事件是啥呢?

WPF GridControl 数据绑定

Dev GridControl