服务器之家

服务器之家 > 正文

如何把Excel数据导入到SQL2008数据库的实例方法

时间:2020-06-11 18:01     来源/作者:MSSQL教程网

如何把Excel数据导入到SQL2008数据库的实例方法

 

复制代码 代码如下:


private void AddManyData_Click(object sender, RoutedEventArgs e)
       {
           OpenFileDialog openFileDialog = new OpenFileDialog();
           openFileDialog.Filter = "Excel文件|*.xls";

           if ((bool)openFileDialog.ShowDialog())  
           {  
                FileInfo fileInfo = new FileInfo(openFileDialog.FileName);  
                string filePath = fileInfo.FullName;  
                string connExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0";

                using (OleDbConnection oleDbConn = new OleDbConnection(connExcel))
                {
                    oleDbConn.Open();

                    //获取excel表  
                    DataTable dt = oleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    //获取Excel表的表名 
                    string tableName = dt.Rows[0][2].ToString().Trim();
                    //去掉空格
                    tableName = "[" + tableName.Replace("'", "") + "]";

                    //利用SQL语句从Excel文件里获取数据  
                     string query = @"SELECT 学号,姓名,公益劳动,电子工艺实习,操作系统 ,计算机组成,数值分析,网络设备与集成,动态网站开发实验周,动态网站开发,均分,排名 FROM ";                  + tableName;
                     DataSet dataSet = new DataSet();

                     using (OleDbCommand oleDbcomm = oleDbConn.CreateCommand())
                     {
                         oleDbcomm.CommandText = query;
                         OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleDbcomm);
                         oleAdapter.Fill(dataSet);
                     }
                     string connStr = "Data Source=HESHUHUA-PC;Initial Catalog=RSMSystem;Integrated Security=True";
                     //利用SqlBulkCopy批量插入数据
                    using (SqlBulkCopy sqlbc = new SqlBulkCopy(connStr))
                    {
                        sqlbc.DestinationTableName = "T_StuScore";
                        // sqlbc.ColumnMappings.Add("学号", "StuNum"),第一个参数对应数据库中的列名,
                        //第二个参数对应数据库中相应表的列名
                        sqlbc.ColumnMappings.Add("学号", "StuNum");
                        sqlbc.ColumnMappings.Add("姓名", "StuName");
                        sqlbc.ColumnMappings.Add("公益劳动", "Activity");
                        sqlbc.ColumnMappings.Add("电子工艺实习", "ElecAct");
                        sqlbc.ColumnMappings.Add("操作系统", "OprationSystem");
                        sqlbc.ColumnMappings.Add("计算机组成", "ComputerMaded");
                        sqlbc.ColumnMappings.Add("数值分析", "DataAnalyze");
                        sqlbc.ColumnMappings.Add("网络设备与集成", "NetWork");
                        sqlbc.ColumnMappings.Add("动态网站开发实验周", "WebWeek");
                        sqlbc.ColumnMappings.Add("动态网站开发", "WebMake");
                        sqlbc.ColumnMappings.Add("均分", "AvScore");
                        sqlbc.ColumnMappings.Add("排名", "StuPaiMing");
                        sqlbc.WriteToServer(dataSet.Tables[0]);
                        MessageBox.Show("数据导入成功!");

                    }
                }  

           }  

       }

 

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
返回顶部