using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
/// <summary>
///
/// </summary>
private ADODB.Connection cn = new ADODB.Connection();
/// <summary>
///
/// </summary>
private ADODB.Recordset rs = new ADODB.Recordset();
/// <summary>
///
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionTimeout = 2;
cn.Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=MHRUSR10","","",-1);
cn.BeginTrans();
updates();
cn.CommitTrans();
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
cn.RollbackTrans();
}
finally
{
if (cn.State == 1)
{
cn.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionTimeout = 2;
cn.Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=MHRUSR10", "", "", -1);
updates();
}
catch(Exception Err)
{
MessageBox.Show(Err.Message);
}
finally
{
if (cn.State == 1)
{
cn.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionTimeout = 2;
cn.Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=MHRUSR10", "", "", -1);
deletes();
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
finally
{
if (cn.State == 1)
{
cn.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionTimeout = 2;
cn.Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=MHRUSR10", "", "", -1);
showdata();
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
finally
{
if (cn.State == 1)
{
cn.Close();
}
}
}
/// <summary>
///
/// </summary>
private void updates()
{
Updates1();
Updates2();
Updates3();
Updates4();
}
/// <summary>
///
/// </summary>
private void Updates1()
{
rs.Open("insert into ta values(1,'王康')", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
///
/// </summary>
private void Updates2()
{
rs.Open("insert into ta values(2,'王康')", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
///
/// </summary>
private void Updates3()
{
rs.Open("insert into ta values(3,'王康')", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
///
/// </summary>
private void Updates4()
{
rs.Open("insert into ta values(1,'王康')", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
/// 删除数据
/// </summary>
private void deletes()
{
rs.Open("delete from ta", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
/// 显示数据
/// </summary>
private void showdata()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.TableName = "XXXX";
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("编号", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("姓名", typeof(System.String)));
rs.Open("select * from ta", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
while (!rs.EOF)
{
dr = dt.NewRow();
dr[0] = Convert.ToString(rs.Fields["ID"].Value);
dr[1] = Convert.ToString(rs.Fields["Name"].Value);
dt.Rows.Add(dr);
rs.MoveNext();
}
DataSet ds = new DataSet();
ds.DataSetName = "XXXX";
ds.Tables.Add(dt);
dataGridView1.DataSource = ds.Tables["XXXX"];
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
/// <summary>
///
/// </summary>
private ADODB.Connection cn = new ADODB.Connection();
/// <summary>
///
/// </summary>
private ADODB.Recordset rs = new ADODB.Recordset();
/// <summary>
///
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionTimeout = 2;
cn.Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=MHRUSR10","","",-1);
cn.BeginTrans();
updates();
cn.CommitTrans();
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
cn.RollbackTrans();
}
finally
{
if (cn.State == 1)
{
cn.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionTimeout = 2;
cn.Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=MHRUSR10", "", "", -1);
updates();
}
catch(Exception Err)
{
MessageBox.Show(Err.Message);
}
finally
{
if (cn.State == 1)
{
cn.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionTimeout = 2;
cn.Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=MHRUSR10", "", "", -1);
deletes();
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
finally
{
if (cn.State == 1)
{
cn.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionTimeout = 2;
cn.Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=MHRUSR10", "", "", -1);
showdata();
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
finally
{
if (cn.State == 1)
{
cn.Close();
}
}
}
/// <summary>
///
/// </summary>
private void updates()
{
Updates1();
Updates2();
Updates3();
Updates4();
}
/// <summary>
///
/// </summary>
private void Updates1()
{
rs.Open("insert into ta values(1,'王康')", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
///
/// </summary>
private void Updates2()
{
rs.Open("insert into ta values(2,'王康')", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
///
/// </summary>
private void Updates3()
{
rs.Open("insert into ta values(3,'王康')", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
///
/// </summary>
private void Updates4()
{
rs.Open("insert into ta values(1,'王康')", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
/// 删除数据
/// </summary>
private void deletes()
{
rs.Open("delete from ta", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
}
/// <summary>
/// 显示数据
/// </summary>
private void showdata()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.TableName = "XXXX";
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("编号", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("姓名", typeof(System.String)));
rs.Open("select * from ta", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
while (!rs.EOF)
{
dr = dt.NewRow();
dr[0] = Convert.ToString(rs.Fields["ID"].Value);
dr[1] = Convert.ToString(rs.Fields["Name"].Value);
dt.Rows.Add(dr);
rs.MoveNext();
}
DataSet ds = new DataSet();
ds.DataSetName = "XXXX";
ds.Tables.Add(dt);
dataGridView1.DataSource = ds.Tables["XXXX"];
}
}
}