日期:2014-05-18  浏览次数:20822 次

dataGridView修改删除(在线等待........)
请问在winform中如删除SQL表中一行数据.怎么修改SQL表一行数据中的其中一个字段及多字段内容

------解决方案--------------------
我也是初学者,今天早上刚学会,这段代码很垃圾,自己写的,你将就着看一下吧~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication4
{
public partial class Form1 : Form
{
private SqlConnection myConn;
private DataTable dt;
private SqlDataAdapter da;
private SqlCommand cmd;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string conn = "Server=127.0.0.1;DataBase=ttt;Uid=sa;Pwd=troika ";
try
{
myConn = new SqlConnection(conn);
myConn.Open();
//SqlCommand sqlCmd = new SqlCommand( "select * from ttt ", myConn);
//da = new SqlDataAdapter( "select * from ttt ", myConn);
cmd = new SqlCommand( "select * from ttt ", myConn);
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
this.dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "读取数据错误,请检查数据库是否出错! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{
da.InsertCommand = new SqlCommandBuilder(da).GetInsertCommand();//需要建立insertcommand
da.Update(dt);
}
catch(Exception exx)//如果不能新增,就是更新
{
da.UpdateCommand = new SqlCommandBuilder(da).GetUpdateCommand();//需要建立updatecommand
da.Update(dt);
}
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
myConn.Close();
}
catch (Exception eeee)
{
MessageBox.Show( "error ", eeee.Message, MessageBoxButtons.OK);
}
}
}
}