Skip to main content
 首页 » 编程设计

c#之向具有标识主键的 sql 数据库添加一行

2024年02月20日15www_RR

我有一个简单的测试数据库,它有一个名为 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 

截图:

请您参考如下方法:

您可能需要检查的三件事:

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;