Sunday, August 12, 2018

Excel Data Base connection string

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.OleDb;

public partial class excelprog : System.Web.UI.Page
{


    OleDbConnection cn;
    OleDbCommand cm;
    OleDbDataReader dr;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("~/file/student.xls") + ";" + "Extended Properties=Excel 8.0;";
        OleDbConnection con = new OleDbConnection(conStr);
        con.Open();

        string k = "select * from [Sheet1$]";
        cm = new OleDbCommand(k, con);
        dr = cm.ExecuteReader();
        GridView2.DataSource = dr;
        GridView2.DataBind();

        dr.Close();


    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        String conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("~/file/student.xls") + ";" + "Extended Properties=Excel 8.0;";
        OleDbConnection con = new OleDbConnection(conStr);
        con.Open();


        string k = "insert into [Sheet1$] values('" + TextBox1.Text + "','" + TextBox2.Text + "'," + TextBox3.Text + " ,'" + TextBox4.Text + "')";
        cm = new OleDbCommand(k, con);
        cm.ExecuteNonQuery();
        TextBox1.Text = "";


    }
}

No comments:

Post a Comment

GRIDVIEW ON ROW DATA BOUND EVENT

 Database Create  Student : roll , name , city , cost  Fix 6 Value  in Database Record  ====================================================...