在具有标识主键的sql数据库中添加一行

时间:2013-02-12 17:35:21

标签: c# sql sql-server

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

我正在尝试向使用sqladapter

创建的数据表添加一个新行

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

"Column "ID" doesn't not allow nulls

我解决了它,但为ID提供了dr["ID"] = 99999

之类的任何值

并且它有效,但是现在当SQL数据库忽略该值并且只是给它正确的数字时,数据表具有99999 ID,这使得它有点令人困惑,如果我尝试插入另一条记录,可能会抛出错误

我该如何解决这个问题?

抱歉:

            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

截图: enter image description here

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

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