関数から戻ろうとしOleDbDataReader
ています。インターネットで検索したところ、いくつかのヘルプが見つかり、コードを作成しました
public IEnumerable<IDataRecord> ImportXLS(string path)
{
string connString = "";
string strFileType = ".xlsx";
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
using (IDataReader xlsReader = cmd.ExecuteReader())
{
while (xlsReader.Read())
{
yield return (IDataReader)xlsReader;
}
}
conn.Close();
}
これは OK It ReturnxlsReader
であり、私はこれをキャッチしようとしxlsReader
ます
public string UploadFile(string tPath, string FileName)
{
string msg = "";
FileImportModule.FileImport OFileImport = new FileImportModule.FileImport();
SqlDataReader reader = (SqlDataReader)OFileImport.ImportXLS(tPath);
var context = new MountSinaiEntities1();
string tableName = context.Tables.Find(1).tableName;
var tableFieldList = from a in context.TablesFields
where a.tableId == 1
select a.fieldName;
//1- SQL Bulk Copy
try
{
SqlConnection con = new SqlConnection(@"Data Source=abhishek-pc;Initial Catalog=MountSinai;User ID=sa;Password=abhi");
con.Open();
SqlBulkCopy bulkcopy = new SqlBulkCopy(con);
{
bulkcopy.DestinationTableName = tableName;
bulkcopy.WriteToServer(reader);
}
con.Close();
}
catch (Exception e)
{
//Handle Exception
}
}
しかし、エラーが発生します
タイプ 'd__0' のオブジェクトをタイプ 'System.Data.SqlClient.SqlDataReader' にキャストできません
その問題の解決策は何ですか? リーダーオブジェクトとして使用する代替手段...