您的位置:首页 > 数据库

using PreSqlData.DataProcess;

2010-03-20 17:33 190 查看
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.SqlClient;

namespace AllSaveGrid

{

public partial class Form1 : Form

{

DataSet ds = new DataSet();

public Form1()

{

InitializeComponent();

GridControl_Bind();

}

public void GridControl_Bind()

{

SqlClass sc = new SqlClass();

//DataSet ds = new DataSet();

ds = sc.SelTable(); //获取DataGridView的数据源

dataGridView1.DataSource = ds.Tables[0];

}

private void button1_Click(object sender, EventArgs e)

{

SqlConnection con = new SqlConnection("server='localhost';database='Work';user='sa';password='123'");

SqlCommand cmd = new SqlCommand();

cmd = con.CreateCommand();

cmd.CommandText = "select * from Table_gongzi";

SqlDataAdapter da = new SqlDataAdapter(cmd);

SqlCommandBuilder cb = new SqlCommandBuilder(da);//使用SqlCommandBuilder的话,首先要求你的表有主键

//然后他会根据你提供的查询语句,生成插入,修改,删除语句

//再根据DataTable对应的数据行状态来调用对应的语句执行数据库操作

da.Update(ds.Tables[0]);

dataGridView1.Update();

}

private void button2_Click(object sender, EventArgs e)

{

DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;

drv.Delete();

SqlConnection con = new SqlConnection("server='localhost';database='Work';user='sa';password='123'");

SqlCommand cmd = new SqlCommand("select * from Table_gongzi", con);

// SqlCommand cmd = new SqlCommand();

// cmd = con.CreateCommand();

// cmd.CommandText = "select * from Table_gongzi";

SqlDataAdapter da = new SqlDataAdapter(cmd);

SqlCommandBuilder cb = new SqlCommandBuilder(da);//必须new SqlCommandBuilder 否则da.Update报错

da.Update(ds.Tables[0]);

dataGridView1.Update();

}

}

}

转载声明: 本文转自 http://blog.csdn.net/shanzhaikaifa/archive/2009/08/13/4443931.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: