1

我有一个简单的测试数据库,它有一个名为 ID 的列,它是一个带有自动标识集的主键。

我正在尝试向使用 sqladapter 创建的数据表添加新行

当我将新行的 ID 列留空时,认为 sql 会添加它,它会显示以下错误:

"Column "ID" doesn't not allow nulls

我解决了它,但给 ID 任何值,比如dr["ID"] = 99999

它起作用了,但是现在 SQL 数据库忽略了该值并只给它正确的数字,但数据表的 ID 为 99999,这使它有点混乱,如果我尝试插入另一条记录,可能会抛出错误

我怎么解决这个问题?

对于那个很抱歉:

            DataRow dr = dt.NewRow();
            dr["Name"] = "fds";//E.Name;
            dr["Age"] = 12;// E.Age;
            dr["DateOfBirth"] = "1-1-1999";// E.DoB;
            //dr["ID"] = 99999;
            dt.Rows.InsertAt(dr, dt.Rows.Count - 1);
            DBOperations.UpdateTable(dt);
            MessageBox.Show("Record added successfully");

    static internal void UpdateTable(DataTable dt)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
        SqlDataAdapter Adapter = new SqlDataAdapter();
        Adapter.SelectCommand = new SqlCommand("select * from Emp",con); 
        Adapter.UpdateCommand = new SqlCommandBuilder(Adapter).GetUpdateCommand();
        Adapter.InsertCommand = new SqlCommandBuilder(Adapter).GetInsertCommand();
        Adapter.DeleteCommand = new SqlCommandBuilder(Adapter).GetDeleteCommand();
        Adapter.Update(dt);
    }

数据库脚本:

USE [HR]
GO
/****** Object:  Table [dbo].[Emp]    Script Date: 2/12/2013 8:10:49 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Emp](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](max) NOT NULL,
    [Age] [int] NOT NULL,
    [DateOfBirth] [date] NOT NULL,
 CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

截屏: 在此处输入图像描述

4

2 回答 2

3

您可能需要检查三件事:

1)你可以试试

dt.Rows.Add(dr);

代替

dt.Rows.InsertAt(dr, dt.Rows.Count - 1);

2) 如果您对表运行 INSERT 语句,SQL Server 是否会创建一个新 ID/完成没有错误的语句?喜欢

INSERT INTO tb_mytable (Name, Age) VALUES ('fds', 12) 

如果不是,则 ID 字段可能有问题。

3)您是否检查了适配器(创建的)插入命令的正确性?

编辑:您也可以-而不是使用CommandBuilder,尝试手动创建插入命令,就像这样,看看会发生什么-如果这工作正常,那么您的CommandBuilder有问题:

string sql  = "INSERT INTO [dbo].[emp] ";
sql += "(Name, Age, DateOfBirth) ";
sql += "Values (@Name, @Age, @DateOfBirth)";

SqlCommand insertCommand = new SqlCommand();
insertCommand.CommandText = sql;

SqlParameter paramName = new SqlParameter();
paramName.ParameterName = "@Name";
paramName.DbType = DbType.AnsiString;
paramName.SqlDbType = SqlDbType.NVarChar;
paramName.SourceVersion = DataRowVersion.Current;
paramName.SourceColumn = "Name";

SqlParameter paramAge = new SqlParameter();
paramAge.ParameterName = "@Age";
paramAge.DbType = DbType.Int32;
paramAge.SqlDbType = SqlDbType.Int;
paramAge.SourceVersion = DataRowVersion.Current;
paramAge.SourceColumn = "Age";

SqlParameter paramDoB = new SqlParameter();
paramDoB.ParameterName = "@DateOfBirth";
paramDoB.DbType = DbType.Date;
paramDoB.SqlDbType = SqlDbType.Date;
paramDoB.SourceVersion = DataRowVersion.Current;
paramDoB.SourceColumn = "DateOfBirth";

insertCommand.Parameters.Add(paramName);
insertCommand.Parameters.Add(paramAge);
insertCommand.Parameters.Add(paramDoB);

Adapter.InsertCommand = insertCommand;
于 2013-02-12T18:22:39.093 回答
1

错误来自DataAdapter,我很确定,而不是来自数据库。请参阅 http://msdn.microsoft.com/en-us/library/ms971502.aspx

于 2013-02-12T18:59:25.270 回答