日期:2014-05-17  浏览次数:20812 次

在datatable中新增记录后,马上修改,就会报错
在datatable中新增记录后,马上修改,就会报如下错误
“违反并发性: UpdateCommand 影响了预期 1 条记录中的 0 条。”
如果修改旧的数据则不报错。

Material表创建语句
CREATE TABLE [Material] (
[i] [int] IDENTITY (1, 1) NOT NULL ,
[ID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[Name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
CONSTRAINT [PK_Material] PRIMARY KEY  CLUSTERED 
(
[ID]
)  ON [PRIMARY] 
) ON [PRIMARY]


代码如下

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 testUpdate
{
    public partial class Form1 : Form
    {
        private SqlConnection nSqlcon;
        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=123";
            nSqlcon = new SqlConnection(strSql);
            nSqlcon.Open();

            strSql = "select * from material order by id";
            SqlDataAdapter sda = new SqlDataAdapter(strSql, nSqlcon);
            nds = new DataSet();
            sda.Fill(nds);
            ndt= nds.Tables[0];