Question:
need sample code to insert data to ms-access through datagridview in c# using data set?
?
2010-10-06 07:01:56 UTC
plz give me a sample code to insert data to ms-access by using data grid view in c#.
Three answers:
?
2010-10-06 11:47:20 UTC
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 queries = new List();

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!");

}

}

}

}
2016-11-03 11:38:50 UTC
i'm with the different poster who reported sticking with the ADO merchandise library. that's time examined, and supported by employing all the products you point out. in case you're already attentive to recordsets, you may desire to have not have been given any subject imposing them in C#. My motto is often : while uncertain, stick to what you realize. sturdy luck. in case you want any help or suggestion, in basic terms permit me understand. Be happy to help in any way i will.
devanathan Thiru
2010-10-06 07:31:53 UTC
you probably wont get the entire code in one shot here..

Search in google "how to read data from gridview column wise"

then use ADODB or any other object to insert data in database


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...