在C#中如何动态创建access数据库并对SQLserver中的数据导入进去

 

 

首先要添加应用com组件 Microsoft ADO Ext.2.8 for DDL and Security

 

引入Using system.ADOX

 private void btnData_Click(object sender, EventArgs e)
        {

            this.saveFileDialog1.DefaultExt = "accdb";  //默认文件的后缀命
       
            this.saveFileDialog1.Filter ="数据文件(*.accdb)|*.accdb|All Filess|(*.accdb,*.XLS,*.DBF)";  //锁打开对话框的后缀名

 

            this.saveFileDialog1.FilterIndex = 2;
           
            saveFileDialog1.RestoreDirectory = true; //记录上次打开的位置
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {

                String localFilePath = saveFileDialog1.FileName.ToString();  //获取所创建的数据库路径


                //获得文件名
                String fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("//") + 1);  //获取文件路径,不带文件名

                String FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("//"));
               
                //给文件名前加上时间  
                String newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;                
                 saveFileDialog1.FileName.Insert(1,"dameng");   给文件加字符串
                 localFilePath = FilePath +"//"+ newFileName;

                //创建新access数据库

                ADOX.Catalog cat = new Catalog();
                cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + localFilePath + "; ");
               

               OleDbConnection oldbconn=null

                try
                {
                    //打开access数据库连接;
                   string accconn = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + localFilePath + " ";
                     oldbcon= new OleDbConnection(accconn );
                    accconn .Open();


                    //创建表结构
                    OleDbCommand oldb = new OleDbCommand("create table config_file_first_kind (ffk_id int  ,first_kind_id char(2),first_kind_name varchar(60),first_kind_salary_id varchar(60),first_kind_sale_id varchar(20))", oldbconn);
                    oldb.ExecuteNonQuery();

 

                    //连接SQLServer数据库
                    string strCon = "data source=.;uidsa;pwd=;initial catalog= your database";
                    SqlConnection con = new SqlConnection(strCon);
                    String sql = "select * from your select table ";

                    DataSet ds = new DataSet();
                    SqlCommand cmd = new SqlCommand(sql, con);

                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);

 


                    try
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {

 

                            string insertsql = string.Format("Insert Into config_file_first_kind (ffk_id,first_kind_id,first_kind_name,first_kind_salary_id,first_kind_sale_id) values ({0},{1},'{2}','{3}','{4}')",
                                dr["ffk_id"].ToString(),
                                dr["first_kind_id"].ToString(),
                                dr["first_kind_name"].ToString(),
                                dr["first_kind_salary_id"].ToString(),
                                dr["first_kind_sale_id"].ToString());

                            //插入数据
                            OleDbCommand com = new OleDbCommand(insertsql, conn);
                            com.ExecuteNonQuery();


                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }

                }
                catch (Exception ex)
                {

                    
                    MessageBox.Show(ex.Message);
                }
                finally
                {

                    accconn.close()
                    MessageBox.Show("数据成功插入");
                }

 



                
            }
               }

 

你可能感兴趣的:(在C#中如何动态创建access数据库并对SQLserver中的数据导入进去)