尝试将excel文件中的数据导入SQL中的临时表 [英] Trying to import data from excel file into temp table in SQL

查看:582
本文介绍了尝试将excel文件中的数据导入SQL中的临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将excel文件中的数据导入sql中的临时表,然后在asp.net的gridview中显示它。



什么我试过了:



i want to import data from an excel file into a temp table in sql and then display it in a gridview in asp.net.

What I have tried:

SqlDataAdapter da = new SqlDataAdapter();

da.InsertCommand = new SqlCommand("SELECT * into #InsertQueues FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;HDR=NO;IMEX=1;Database= " + filePath + "'" + @"SELECT * FROM[" + getExcelSheetName + @"')");


da.SelectCommand = new SqlCommand("Select * from #InsertQueues");

vid.Open();

da.InsertCommand.ExecuteNonQuery();

da.SelectCommand.ExecuteNonQuery();

DataSet ds = new DataSet();

da.Fill(ds);

GridView2.DataSource = ds;

GridView2.DataBind();

vid.Close();



错误:ExecuteNonQuery:连接属性尚未初始化。


Error: ExecuteNonQuery: Connection property has not been initialized.

推荐答案

ExecuteNonQuery: Connection property has not been initialized.




da.SelectCommand = new SqlCommand("Select * from #InsertQueues", vid);



无需打开和关闭连接。 SqlDataAdapter 在内部执行此操作。此外,此处不需要 ExecuteNonQuery ,所有内容均由 Fill 方法处理。


And no need to open and close the connection. SqlDataAdapter does this internally. Also, ExecuteNonQuery is not required here, everything is handled by Fill method.


这篇关于尝试将excel文件中的数据导入SQL中的临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆