问题描述
以下是我的代码,我想上传一个excel文件作为输入。
然后,
步骤1:我已将文件保存在一个路径中。
步骤2:读取Excel文件。
步骤3:将Excel转换为数据集。
步骤4:使用bulkinsert查询将数据集插入表格。
执行行代码时出现未指定的错误 Connection1.Open() ;
现在代码部署在共享点网站投掷错误...
请帮助我。
Hi,
Below is my code, I want upload a excel file as input.
Then,
step 1:I have saved file in one path.
step 2:Read Excel file.
step 3:Convert excel into data set.
step 4:Insert data set into table using bulkinsert query.
Unspecified error occur while executing the code on the line Connection1.Open();
now code is deployed in share point site throwing error...
Please help me.
private static DataSet GetExcelAsDataSet(string fileName, bool firstRowIsHeader)
{
OleDbConnection excelConnection = null;
OleDbDataAdapter adapter = null;
DataSet ds = new DataSet();
System.Data.DataTable dtTables = new System.Data.DataTable();
//fileName = "C:\\myfolder\\UserInformationList.xlsx";
string myconnection1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HDR=YES;'";
using (OleDbConnection Connection1 = new OleDbConnection(myconnection1))
{
Connection1.Open();
//to get the schema of the workbook.
dtTables = excelConnection.GetSchema();
//get the tables in the workbook
dtTables = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String[] excelSheets = null;
if ((dtTables != null))
{
excelSheets = new String[dtTables.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach (DataRow row in dtTables.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
}
//prepare dataset from the tables in the workbook
foreach (string sheet in excelSheets)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = excelConnection;
cmd.CommandText = "Select * from [" + sheet + "]";
System.Data.DataTable dtItems = new System.Data.DataTable();
string SheetName = sheet.Trim('\'');
SheetName = SheetName.Substring(0, SheetName.IndexOf("$"));
dtItems.TableName = SheetName;
if (!ds.Tables.Contains(SheetName))
{
adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(dtItems);
ds.Tables.Add(dtItems);
}
}
}
return ds;
}
注意:我在C#web应用程序中使用了相同的代码,工作正常。
Note: i have used same code in C# web application its working fine.
推荐答案
注意:我在C#web中使用了相同的代码应用工作正常。
Note: i have used same code in C# web application its working fine.
<identity impersonate="true" /> to <identity impersonate="false" />
。
现在代码工作正常。
谢谢你们
in my share point web config file.
Now the code was working fine.
Thank you guys
这篇关于System.Data.OleDb.OleDbExcepti ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!