Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Import Data from Excel to SQL Server using C# in ASP. NET
WhatsApp
Pintoo Yadav
Aug 11
2015
1.8
k
0
2
string
SourceConstr = @
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"
+ +
"';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'"
;
OleDbConnection con =
new
OleDbConnection(SourceConstr);
string
query =
"Select * from [Sheet1$]"
;
OleDbDataAdapter data =
new
OleDbDataAdapter(query, con);
data.Fill(dtExcel);
fileName = FileUpload1.ResolveClientUrl(FileUpload1.PostedFile.FileName);
int
count = 0;
DataClassesDataContext conLinq =
new
DataClassesDataContext(
"Data Source=server name;Initial Catalog=Golivecode.com;Integrated Security=true"
);
try
{
DataTable dtExcel =
new
DataTable();
string
SourceConstr = @
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"
+ fileName +
"';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'"
;
OleDbConnection con =
new
OleDbConnection(SourceConstr);
string
query =
"Select * from [Sheet1$]"
;
OleDbDataAdapter data =
new
OleDbDataAdapter(query, con);
data.Fill(dtExcel);
for
(
int
i = 0; i < dtExcel.Rows.Count; i++)
{
try
{
count += conLinq.ExecuteCommand(
"insert into table name values("
+ dtExcel.Rows[i][0] +
","
+ dtExcel.Rows[i][1] +
",'"
+ dtExcel.Rows[i][2] +
"',"
+ dtExcel.Rows[i][3] +
")"
);
}
catch
(Exception ex)
{
continue
;
}
}
if
(count == dtExcel.Rows.Count)
{
< --Success Message-- >
}
else
{
< --Failure Message-- >
}
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
conLinq.Dispose();
}
ASP. NET
Import Data
Import Data from Excel to SQL
C#
Up Next
Import Data from Excel to SQL Server using C# in ASP. NET