ASP.NET導入excel數據到Sql server2005

//將excel中的內容導入到數據庫中
//得到要導入的excel文件的文件路徑
        String importurl = this.FileUpload_ImportRecord.PostedFile.FileName;


      //連接數據庫,並把excel插入到相應的表中
        SqlConnection con = new SqlConnection("server=localhost\\SQLEXPRESS;Database=考勤系統;uid=sa;password=111111");
        String sqlstr="insert into PeopleManage(Partment,PeopleNum,LoginNum,Name,Sex,Date,Bus,Rest,VersionNum,Kong1,Kong2,
        Kong3,Kong4,Kong5) select 部門,姓名,編號,登記號碼,卡片號碼,考勤制度,默認考勤,默認公休,性別,入職日期,職務,出生日期,證件號,
       聯繫電話 from openrowset('microsoft.jet.oledb.4.0','excel 5.0;IMEX=1;HDR=YES;database=d:\\111.xls',[sheet1$])";
        SqlCommand cmd = new SqlCommand(sqlstr, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();



SQL Server 阻止了對組件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的訪問


解決這個問題的方法:
啓用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1  
reconfigure  
exec sp_configure 'Ad Hoc Distributed Queries',1  
reconfigure



完成後,關閉Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0  
reconfigure  
exec sp_configure 'show advanced options',0  
reconfigure 


發佈了37 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章