0
我有我使用SqlBulkCopy的上傳到SQLSERVER表Excel工作表,我有重複的條目被忽略,但我也想顯示重複的條目適用於數據庫表上的索引重複行這在網格視圖中被忽略。查詢兩個數據表中找到
protected void Button1_Click(object sender, EventArgs e)
{
string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
string strFileName = FileUpload1.PostedFile.FileName.ToString();
FileUpload1.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType));
string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType);
string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+strNewPath +"; Extended Properties=Excel 8.0;");
//string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\myFolder\\Book1.xls;" + "Extended Properties=Excel 8.0;");
// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Sheet1$]", connection);
OleDbCommand command1 = new OleDbCommand("select count(*) from [Sheet1$]",connection);
//Sql Server Table DataTable
DataTable dt4=new DataTable();
SqlCommand cmd4 = new SqlCommand("select id,data from ExcelTable", con);
try
{
SqlDataAdapter da4 = new SqlDataAdapter(cmd4);
da4.Fill(dt4);//sql table datatable
}
catch { }
//excelsheet datatable
DataTable oltlb = new DataTable();
OleDbCommand olcmd = new OleDbCommand("select id,data from [Sheet1$]", connection);
try
{
OleDbDataAdapter olda = new OleDbDataAdapter(olcmd);
olda.Fill(oltlb); //excel table datatable
}
catch { }
var matched = (from table1 in dt4.AsEnumerable()
join table2 in oltlb.AsEnumerable() on
table1.Field<int>("ID") equals table2.Field<int>("ID")
where table1.Field<string>("Data") == table2.Field<string>("Data")
select table1);
DataTable dthi5 = new DataTable();
dthi5 = matched.CopyToDataTable();
GridView1.DataSource = dthi5;
GridView1.DataBind();
GridView1.DataSource = matched.ToList();
錯誤: 指定的轉換是無效的。 table1.Field(「ID」)等於table2.Field(「ID」)
還是同樣的錯誤 – Arbaaz
哪些數據類型是兩個表的第一列? –
第一個表是sql表int,第二個表是我從excel表導出的一個表 – Arbaaz