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