日期:2014-05-18 浏览次数:20861 次
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private OleDbConnection conn;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
conn = DBOperator.GetConn();
if (conn == null)
{
MessageBox.Show("数据库连接失败");
return;
}
OleDbDataAdapter da = new OleDbDataAdapter("select * from person",conn);
DataSet ds = new DataSet();
da.Fill(ds, "person");
dataGridView1.DataSource = ds.Tables["person"];
conn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
conn = DBOperator.GetConn();
OleDbDataAdapter da = new OleDbDataAdapter("select * from person", conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(da);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
DataSet ds = new DataSet();
da.Fill(ds,"person");
DataRow dr =ds.Tables["person"].NewRow();
dr["name"] = textBox1.Text;
dr["password"] =textBox2.Text;
dr["age"] = textBox3.Text;
ds.Tables["person"].Rows.Add(dr);
da.Update(ds, "person");
// dataGridView1.Update(); 此语句无效!
dataGridView1.DataSource = ds.Tables["person"]; //datagridview首行ID不显示
}
}
}