Take Two text boxes like id,name and take one button
Copy these code in Button Click event..
if (TextBox1.Text != string.Empty && TextBox2.Text != string.Empty)
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:/Raj Collection For ASP Examples/Raj Practise Examples/Excel InsertData/Test3.xls';Extended Properties=Excel 8.0;");
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "Insert into [Sheet1$] (TitleId,TitleName) values('" + TextBox1.Text + "','" + TextBox2.Text + "')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
Copy these code in Button Click event..
if (TextBox1.Text != string.Empty && TextBox2.Text != string.Empty)
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:/Raj Collection For ASP Examples/Raj Practise Examples/Excel InsertData/Test3.xls';Extended Properties=Excel 8.0;");
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "Insert into [Sheet1$] (TitleId,TitleName) values('" + TextBox1.Text + "','" + TextBox2.Text + "')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
No comments:
Post a Comment