如何更新数据库呢?我是通过sda.Update(ndt);来更新的 private void Form1_Load(object sender, EventArgs e) { //打开表 string strSql="Server=.;Database=weight;Uid=sa;Pwd=000"; nSqlcon = new SqlConnection(strSql); nSqlcon.Open(); strSql = "select * from material order by id"; sda = new SqlDataAdapter(strSql, nSqlcon); SqlCommand scmd = new SqlCommand("select * from material order by id", nSqlcon); sda.SelectCommand = scmd; SqlCommandBuilder scb = new SqlCommandBuilder(sda); nds = new DataSet(); sda.Fill(nds); ndt = nds.Tables[0]; dataGridView1.DataSource = ndt;
{
//打开表
string strSql="Server=.;Database=weight;Uid=sa;Pwd=000";
nSqlcon = new SqlConnection(strSql);
nSqlcon.Open();
strSql = "select * from material order by id";
sda = new SqlDataAdapter(strSql, nSqlcon);
SqlCommand scmd = new SqlCommand("select * from material order by id", nSqlcon);
sda.SelectCommand = scmd;
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
nds = new DataSet();
sda.Fill(nds);
ndt = nds.Tables[0];
dataGridView1.DataSource = ndt;
DataSet emailAdd=new DataSet();
SqlDataAdapter emailAdapter=new SqlDataAdapter(emailSql,myConn);
SqlCommandBuilder cb=new SqlCommandBuilder(emailAdapter);
emailAdapter.Fill(emailAdd,"address");
myConn.Close();
DataTable myDt=emailAdd.Tables["address"];
myDt.PrimaryKey=new DataColumn[]{myDt.Columns["email"]};
......//修改myDs数据
emailAdapter.Update(emailAdd,"address");
指定的数据库的那行数据凭什么消失啊?
sda.Update(ndt);
这句
看一下你sda的DeleteCommand值是否是正确的delete语句
而且,你只需要ndr.Delete()就可以了
具体你看下你ndr对应的RowState是否标记为删除就知道了
用DataAdapter更新数据库有两个要求
1、有对应的Insert/Update/DeleteCommand
2、相对应的DataRow标记为已增加/修改/删除
ndr = ndt.Rows[dataGridView1.SelectedRows[0].Index];
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
ndr.Delete();
sda.Update(ndt);
可以删除掉,数据库中也被删除了.
断点打在 sda.Update(ndt),查看sda的DeleteCommand值,发现为null值.ndr对应的RowState值为deleted.
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 testUpdateRow
{
public partial class Form1 : Form
{
private SqlConnection nSqlcon;
private SqlDataAdapter sda;
private DataSet nds;
private DataTable ndt;
private DataRow ndr; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//打开表
string strSql="Server=.;Database=weight;Uid=sa;Pwd=000";
nSqlcon = new SqlConnection(strSql);
nSqlcon.Open();
strSql = "select * from material order by id";
sda = new SqlDataAdapter(strSql, nSqlcon);
SqlCommand scmd = new SqlCommand("select * from material order by id", nSqlcon);
sda.SelectCommand = scmd;
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
nds = new DataSet();
sda.Fill(nds);
ndt = nds.Tables[0];
dataGridView1.DataSource = ndt;
} private void button1_Click(object sender, EventArgs e)
{
//新增一条记录
ndr = ndt.NewRow();
ndr["ID"] = textBox2.Text;
ndr["Name"] = textBox3.Text;
ndt.Rows.Add(ndr);
sda.Update(ndt);
} private void button2_Click(object sender, EventArgs e)
{
//修改一条记录
ndr = ndt.Rows[dataGridView1.SelectedRows[0].Index];
ndr.BeginEdit();
ndr["Name"] = textBox3.Text;
ndr.EndEdit();
sda.Update(nds);
} private void button3_Click(object sender, EventArgs e)
{
//删除一条数据
ndr = ndt.Rows[dataGridView1.SelectedRows[0].Index];
ndt.Rows.Remove(ndr);
sda.Update(ndt);
}
}
}
把你的Remove改成 DataRow.Delete(这时DataRowState是Deleted,且可被DataTable.Rows遍历到),然后更新,行会被ado执行实际删除
感谢phommy的关注
请教 http://www.cnblogs.com/jhxk/articles/2328744.html 这篇文章里的删除方法改如何理解呢?网上好多介绍remove和removeat方法的文章,难道是这些文章错了?
感谢phommy的关注
请教 http://www.cnblogs.com/jhxk/articles/2328744.html 这篇文章里的删除方法改如何理解呢?网上好多介绍remove和removeat方法的文章,难道是这些文章错了?用处不同而已
如果只是把DataTable当数据载体用、不回写数据库,一般用Remove;增加Delete方法(只是我觉得,毕竟无法知道微软的人的真正想法)主要目的就是用于你这样的回写数据库需求的
Delete 是标记DataTable 行的DataRowState 状态为Deleted的并非移除.调用DataTable 的 AccepteChanges时才进行Remove掉(移除掉).Update 是根据DataTable行的DataRowState状态,更新回数据库
Added 对应插入
Modified对应修改
Deleted对应删除
2.datatabel.Rows.Remove(datarow);//将此行DataRowState标记为Detached
3.datatable.AcceptChanges();//将DataRowState标记为Deleted的行的更改为Detached
4.SqlDataAdapter.Update(datatable);//更新数据库,将DataRowState标记为Deleted的行从数据库中删除所以
dataRow.Delete();
SqlDataAdapter.Update(datatable);
可以删除数据库中的数据datatabel.Rows.Remove(datarow);
SqlDataAdapter.Update(datatable);
不能删除数据库中的数据dataRow.Delete();
datatable.AcceptChanges();
SqlDataAdapter.Update(datatable);
不能删除数据库中的数据