使用LinqToSql将数据从excel上传到sql server [英] Upload data from excel to sql server With LinqToSql
本文介绍了使用LinqToSql将数据从excel上传到sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,我必须将数据从excel上传到sql server。
我正在使用 LinqToSql
这是我的代码
In my Project i have to upload data from excel to sql server.
I am using LinqToSql
this is my code
public ActionResult UploadDataToSql()
{
CustomerAccount account = new CustomerAccount();
string fileName = @"C:\Users\MR Right\Desktop\Customers.xlsx";
var excel = new ExcelQueryFactory(fileName);
var dataContent = from c in excel.Worksheet<CustomerSheet>("Customers")
select c;
excel.AddMapping<CustomerSheet>(x => x.ID, account.SFId);
excel.AddMapping<CustomerSheet>(x => x.Name, account.AccountName);
excel.AddMapping<CustomerSheet>(x => x.BillingStreet, account.StreetAddress1);
excel.AddMapping<CustomerSheet>(x => x.BillingState, account.State.ToString());
excel.AddMapping<CustomerSheet>(x => x.BillingPostal, account.ZipCode);
excel.AddMapping<CustomerSheet>(x => x.Phone, account.Country);
excel.AddMapping<CustomerSheet>(x => x.CreatedDate, account.CreatedDate.ToString());
excel.AddMapping<CustomerSheet>(x => x.LastModifiedDate, account.LastModifiedDate.ToString());
//Checking the datacontent before accessing
if (dataContent != null)
{
//reading content from the excel comes here
foreach (var a in dataContent)
{
//all logics and save to database
}
}
return View("~/Views/Admin/UploadData.cshtml");
}
In Line
In Line
foreach (var a in dataContent)
in ai am null value
表示使用此代码我无法阅读excel
可以帮助一下
谢谢
in a i am getting null values
means with this code i am unable to read excel
can some one help
Thanks
推荐答案
请参考以下链接。
使用ASP中的C#从excel导入数据到SQL服务器。 NET [ ^ ]
http://www.aspsnippets.com/Articles/Read-and-Import-Excel-Sheet-into-SQL-Server-Database -in-ASP.Net.aspx [ ^ ]
使用C#将MS Excel数据导入SQL Server表 [ ^ ]
HTTP ://www.c-sharpcorner.com/UploadFile/99bb20/import-excel-data-to-sql-server-in-Asp-Net/ [ ^ ]
这些会有所帮助。
问候,
Praneet Nadkar
Hi,
Please refer to the following links.
Import Data from excel to SQL server using C# in ASP. NET[^]
http://www.aspsnippets.com/Articles/Read-and-Import-Excel-Sheet-into-SQL-Server-Database-in-ASP.Net.aspx[^]
Import MS Excel data to SQL Server table using C#[^]
http://www.c-sharpcorner.com/UploadFile/99bb20/import-excel-data-to-sql-server-in-Asp-Net/[^]
These would help.
Regards,
Praneet Nadkar
这篇关于使用LinqToSql将数据从excel上传到sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文