DataTable dt = null;
if (FileUpload1.HasFile)
{
filename = FileUpload1.FileName;
filename = AppDomain.CurrentDomain.BaseDirectory + @"" + filename;
var fileExtension = Path.GetExtension(filename).Substring(1);
if (fileExtension == "xlsx")
{
{
DataSet myDataset = new DataSet();
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + @";Extended Properties=""Excel 12.0 Xml;HDR=YES""";
OleDbConnection myData = new OleDbConnection(strConn);
try
{
myData.Open();
}
catch (Exception ex)
{
try
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended Properties=Excel 8.0;HDR=YES;";
myData = new OleDbConnection(strConn);
myData.Open();
}
catch (Exception ex2)
{
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + @";Extended Properties=""HTML Import;HDR=YES;IMEX=1"";";
myData = new OleDbConnection(strConn);
try
{
myData.Open();
}
catch (Exception ex3)
{
}
}
}
if (myData.State.ToString() != "Closed")
{
int i = 0;
DataSet ds = new DataSet();
List<string> queryList = new List<string>();
foreach (DataRow row in myData.GetSchema("tables").Rows)
{
try
{
i++;
string name = row[2].ToString().Replace("''", "'").TrimEnd('_');
OleDbDataAdapter d = new OleDbDataAdapter("SELECT * from [" + name + "]", strConn);
d.Fill(ds);
dt = ds.Tables[0].Copy();
dt.Namespace = name;
myDataset.Tables.Add(dt);
}
catch (Exception ex)
{
}
break;
}
}
}
}
</string></string>
Then you can insert Data into your SQL Database.
BU_UtilDB db = new BU_UtilDB();
if (existingrow)
{
try
{
string query = "INSERT INTO [dbo].[Fr_Endkunde]([Anrede],[anrede_id]"
+ ",[Firma],[Geburtsdatum],[Name],[Ort],[PLZ] ,[Postfach],[Strasse],[Titel],[Vermittlernummer],[Vorname],[Land])"
+ "VALUES('" + dr["Anrede"] + "','" + dr["anrede_id"] + "','" + dr["Firma"] + "','" + datetime + "',"
+ "'" + dr["Nachname"] + "','" + dr["Ort"] + "','" + dr["PLZ"] + "','" + dr["Postfach"] + "','" + dr["Strasse"] + "','" + dr["Title"] + "',"
+ "'" + dr["Vermittlernummer"] + "','" + dr["Vorname"] + "','" + dr["Land"] + "')";
BU_UtilDB.ExecuteReader(query);
}
catch (Exception ex)
{
}
}
}
In SQL You use your own Column Name. Hope you will got your answer.