You must Sign In to post a response.
  • Category: ASP.NET

    Export Binry Data from Database to gridview

    Hello Friends

    I have binary data in database which is in .xls or .xlsx format.

    Now i have to import data in gridview or in dataset through code

    please remind data is in binary format as it has been uploaded in sql table

    thanks
    Anita
  • #768890
    Hi,

    To Export data from excel to SQL you have to connect with OLEDB Provider and insert the data into database like below


    private void InsertIntoDB()
    {
    int count=0;

    SqlConnection con = new SqlConnection("DataBase=ENGSOFT;User Id=sa;Password=P@ssword9");
    try
    {
    con.Open();
    SqlCommand cmd;
    DataTable dt = Session["Table"] as DataTable;


    if (dt.Rows.Count > 0)
    {
    //Read each and everyrow of datatable and insert each and every row to SQL DataBase.
    foreach (DataRow dr in dt.Rows)
    {
    string Ename = dr["ENAME"].ToString();
    string Job = dr["JOB"].ToString();
    string MGR = dr["MGR"].ToString();
    string Sal = dr["SAL"].ToString();
    string comm = dr["COMM"].ToString();

    //normal insert query.

    cmd = new SqlCommand("INSERT INTO EMP(ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(@ename,@job,@mgr,@hiredate,@sal,@comm,@deptno)", con);
    cmd.Parameters.AddWithValue("@ename", Ename);
    cmd.Parameters.AddWithValue("@job", Job);
    cmd.Parameters.AddWithValue("@mgr", MGR);
    cmd.Parameters.AddWithValue("@hiredate", DateTime.Now);
    cmd.Parameters.AddWithValue("@sal", Sal);
    cmd.Parameters.AddWithValue("@comm", comm);
    cmd.Parameters.AddWithValue("@deptno", "1");

    int n = cmd.ExecuteNonQuery();
    count = count + n;

    }
    }
    }
    catch (Exception ex)
    {
    }
    finally
    {
    con.Close();
    con.Dispose();
    lblResult.Text = count + " Records Inserted Successfully...!!!";
    }
    }


    for binary format have you look at the below link may be this might be helpful to you.
    http://www.sqlservercentral.com/blogs/steve_jones/2013/02/17/inserting-binary-data/

    Hope this helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #768896
    Hai Anita,
    First you need to get the binary data and then convert it and then you can use it.
    Below is the link which will give you some idea of how to do that:

    http://www.csharpstar.com/csharp-program-to-convert-binary-string-to-integer/

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments