ASP.NET导入excel数据到Sql server2005

来源:互联网 发布:android源码 小米商城 编辑:程序博客网 时间:2024/04/29 11:16

//将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 


原创粉丝点击