using System.Data.SqlClient;using System.Data.OleDb;using System.Configuration;public partial class _Default : System.Web.UI.Page{ SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()); SqlCommand sqlcmd = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(); DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { lblmsg.Text = ""; LoadGrid(); } protected void Button1_Click(object sender, EventArgs e) { ExportToSQLServer("D:\\Sample.xlsx"); } void ExportToSQLServer(string path) { System.Data.DataSet DtSet = null; System.Data.OleDb.OleDbDataAdapter MyCommand = null; try { string excelconstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';Extended Properties=Excel 12.0;"; OleDbConnection excelcon = new OleDbConnection(excelconstr); MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", excelconstr); DtSet = new System.Data.DataSet(); MyCommand.Fill(DtSet, "[Sheet1$]"); dt = DtSet.Tables[0]; //Here data are filled in dt so you can use loop to get field and insert in various table or single table only then use below sql bulk copy SqlBulkCopy sqlBulk = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["Con"].ToString()); sqlBulk.DestinationTableName = "emp"; sqlBulk.WriteToServer(dt); excelcon.Close(); lblmsg.Text = "Excel data upload successfully"; } catch (Exception ex) { lblmsg.Text = ex.ToString(); } } }
DataSet objDS = new DataSet(); objDS.ReadXml(new XmlTextReader(new StringReader(xml)));DataTable objDTable= objDS.Tables[0];//OR use XMLdocument to read XMLXmlDocument objDOM= MethodReturnsXmlDocument();DataSet objDS= new DataSet()objDS.ReadXml(new XmlNodeReader(objDOM));