locked
Insert data to ms access from datagridview using dataset RRS feed

  • Question

  • User-1463177530 posted
     

    please give me sample code for insert data to MS ACCESS  from grid view using dataset

    Friday, October 1, 2010 12:00 PM

Answers

All replies

  • Saturday, October 2, 2010 4:39 AM
  • User-1463177530 posted

    thanks for reply.

    but i need code for insert to msaccess data ..

    I have tried with data set same as sql database but it not worked for me in msaccess.

    Sunday, October 3, 2010 1:20 PM
  • User1716267170 posted

    I think this link will help you with adding data to Access database via ADO.NET:

    http://msdn.microsoft.com/en-us/library/ms971485.aspx 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 7, 2010 4:23 AM
  • User1949625403 posted

    hi ekreddy try this code may be it helps you

    try this link

    http://msmvps.com/blogs/deborahk/archive/2009/08/25/update-a-microsoft-access-database.aspx

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;
    using System.Data;
    
    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public OleDbConnection con;
    public Form1()
    {
    InitializeComponent();
    string databaseName="c:\\myData.mdb";
    string conStr= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + databaseName;
    conStr += ";User ID=" + userId;
    conStr += ";password=" + DbPassword;
    con = new OleDbConnection(conStr);
    }
    public bool ExecuteBatchUpdate(string[] queries)
    {
    con.Open();
    OleDbTransaction trans = con.BeginTransaction();
    try
    {
    OleDbCommand cmd;
    foreach (string query in queries)
    {
    cmd = new OleDbCommand(query, con, trans);
    cmd.ExecuteNonQuery();
    }
    trans.Commit();
    con.Close();
    return true;
    }
    catch (Exception)
    {
    trans.Rollback();
    con.Close();
    return false;
    }
    
    }
    private void button1_Click(object sender, EventArgs e)
    {
    List<string> queries = new List<string>();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    if (!row.IsNewRow())
    {
    queries.Add("Insert Into Table1 (Field1,Field2,Field3) values ('" + row.Cells[0].Value.ToString() + "','" + row.Cells[1].Value.ToString() + "','" + row.Cells[2].Value.ToString() + "')");
    }
    }
    if (ExecuteBatchUpdate(queries.ToArray()))
    {
    MessageBox.Show("Update Successful");
    }
    else
    {
    MessageBox.Show("Update Failed!");
    }
    }
    }
    }



    Thursday, October 7, 2010 4:31 AM
  • User-1199946673 posted

    I have tried with data set same as sql database but it not worked for me in msaccess.
     

    You need to use the System.Data.OleDb NameSpace, instead of the System.Data.SqlClient

    http://www.mikesdotnetting.com/Article/78/AccessDataSource-SqlDataSource-and-connecting-to-Access-databases-in-ASP.NET
    http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access

    Thursday, October 7, 2010 6:06 AM