程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL2008 >> 若何把Excel數據導入到SQL2008數據庫的實例辦法

若何把Excel數據導入到SQL2008數據庫的實例辦法

編輯:MSSQL2008

若何把Excel數據導入到SQL2008數據庫的實例辦法。本站提示廣大學習愛好者:(若何把Excel數據導入到SQL2008數據庫的實例辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是若何把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.WordStr("'", "") + "]";

                    //應用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("數據導入勝利!");

                    }
                }  

           }  

       }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved