在ASP.net数据库中插入自动增量
我正在尝试使用以下查询注册用户:在ASP.net数据库中插入自动增量,asp.net,mysql,sql,database,auto-increment,Asp.net,Mysql,Sql,Database,Auto Increment,我正在尝试使用以下查询注册用户: "INSERT INTO Users (Username, Password, FirstName, Surname, Age, HouseNumber, StreetName, Town, County, Postcode, TelephoneNumber, isBanned, isAdmin) VALUES ('" + textboxUsername.Text + "','
"INSERT INTO Users (Username, Password, FirstName, Surname, Age, HouseNumber,
StreetName, Town, County, Postcode, TelephoneNumber,
isBanned, isAdmin)
VALUES ('" + textboxUsername.Text + "','" + textboxPassword.Text + "','" +
textboxFirstName.Text + "','" + textboxSurname.Text + "','" + textboxAge.Text + "','" + textboxHouseNumber.Text +
"','" + textboxStreetName.Text + "','" +
textboxTown.Text + "','" + textboxCounty.Text + "','" +
textboxPostcode.Text + "','" + textboxTelephoneNumber.Text +
"', 'false', 'false')", con);
数据库按照您的预期进行设置,只使用上述值和UserID,UserID应该是使用标识规范的自动增量值。但是,我得到以下错误:
Cannot insert the value NULL into column 'UserID', table 'myFilePath'; column does not allow nulls. INSERT fails.
为什么会这样?我不能告诉它为UserID插入任何内容,因为数据库需要处理这个问题,当前UserID是主键,因此不能为null。我做错了什么?这在几个月前还可以使用您可以使用SQL自动增量:
CREATE TABLE Users
(
UserID int NOT NULL AUTO_INCREMENT,
Username varchar(255) NOT NULL,
Password varchar(255) ,
.....
PRIMARY KEY (UserID)
)
使用自动增量定义表格:
CREATE TABLE Users
(
UserID int NOT NULL AUTO_INCREMENT,
Username varchar(255) NOT NULL,
Password varchar(255) ,
.....
PRIMARY KEY (UserID)
)
然后插入:
INSERT INTO Users (Username, Password, FirstName, Surname, Age, HouseNumber,
StreetName, Town, County, Postcode, TelephoneNumber,
isBanned, isAdmin)
确保列上有序列,如果没有,请添加一个序列:
CREATE SEQUENCE yourschemaname.Users
START WITH 1
INCREMENT BY 1;
GO
您还可以在查询中将
id
提到为NULL
。使用自动递增
将其填充:
INSERT INTO Users
(UserID, Username, Password, FirstName, Surname, Age, HouseNumber, StreetName,
Town, County, Postcode, TelephoneNumber, isBanned, isAdmin
)
VALUES
(NULL,'" + textboxUsername.Text + "','" +
textboxPassword.Text + "','" + textboxFirstName.Text + "','" +
textboxSurname.Text + "','" + textboxAge.Text + "','" + textboxHouseNumber.Text + "','" +
textboxStreetName.Text + "','" + textboxTown.Text + "','" + textboxCounty.Text + "','" +
textboxPostcode.Text + "','" + textboxTelephoneNumber.Text + "', 'false', 'false')",
con);
还要检查字段UserID
的数据类型,它应该是这样的:
UserID INT unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY;
不敢相信我必须这样做,但数据库连接路径是错误的。对不起,伙计们,我丢了一封信。我会投票给每个人,然后自己“回答”,让大家看到。你能发布
用户的模式吗?可能您忘记设置自动增量属性。请检查是否已设置自动增量。请看这个例子:@JohnTotetWoo我不知道怎么做抱歉,我相对来说是新手,但我将“标识规范”设置为“是”(Is Identity)是的,Identity递增为1,Identity Seed为1通过包含userid
和null值编写查询与不包含userid
编写查询是一样的。我认为OP忘记了设置表中的userid自动递增属性。我认为OP是在请求MySQL查询。