简体   繁体   English

Mysql主键id问题

[英]Mysql primary key id issues

Basically I'm doing a system where the user enters a Name,E-mail,password & User type and these inputs are to be saved into my local database.基本上我正在做一个系统,用户输入姓名、电子邮件、密码和用户类型,这些输入将保存到我的本地数据库中。 I am trying to create a Primary Key ID that automatically generates and increments the ID, so for the first input 1, second 2, third 3, etc.我正在尝试创建一个自动生成和递增 ID 的主键 ID,因此对于第一个输入 1、第二个 2、第三个 3 等。

Instead I'm getting a bunch of letters and random numbers such as the following;相反,我得到了一堆字母和随机数,如下所示; d7a8675a-c7cf-406a-983f-370d5b96fa62 d7a8675a-c7cf-406a-983f-370d5b96fa62

Here's what I've written on the Visual Studio Database Designer这是我在 Visual Studio 数据库设计器上写的内容

CREATE TABLE [dbo].[Users] (
    [Id]       int IDENTITY(1,1) PRIMARY KEY,
    [UserName] NCHAR (50)    NULL,
    [Email]    NCHAR (50)    NULL,
    [Password] NCHAR (50)    NULL,
    [Role]     NCHAR (15)    NULL
);

Also I forgot to mention that when I am accessing my Register.aspx page where the user is able to fill in the fields to be inserted into the DB i am calling the following on Click of the Register Button, could it be causing a problem wih my SQL code?另外我忘了提到,当我访问我的 Register.aspx 页面时,用户可以在其中填写要插入数据库的字段,我在单击注册按钮时调用以下内容,是否会导致问题我的 SQL 代码?

String insertQuery = "insert into Users (Id,UserName,Email,Password,Role) values (@Id ,@Uname ,@email ,@password ,@role)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Id", newGUID.ToString());
com.Parameters.AddWithValue("@Uname",TextBoxUN.Text);
com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@password", TextBoxPass.Text);
com.Parameters.AddWithValue("@role", DropDownListRole.SelectedItem.ToString());

First change table definition to:首先将表定义更改为:

CREATE TABLE [dbo].[Users] (
    [Id]       int IDENTITY(1,1) PRIMARY KEY NOT NULL,
    [UserName] NCHAR (50)    NULL,
    [Email]    NCHAR (50)    NULL,
    [Password] NCHAR (50)    NULL,
    [Role]     NCHAR (15)    NULL
);

and your code:和你的代码:

String insertQuery = "insert into Users (UserName,Email,Password,Role) values (@Uname ,@email ,@password ,@role)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Uname",TextBoxUN.Text);
com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@password", TextBoxPass.Text);
com.Parameters.AddWithValue("@role", DropDownListRole.SelectedItem.ToString());

You need to skip saving com.Parameters.AddWithValue("@Id", newGUID.ToString());您需要跳过保存com.Parameters.AddWithValue("@Id", newGUID.ToString()); and allow IDENTITY to fill the values.并允许IDENTITY填充值。

Your primary key definition is wrong Try this:你的主键定义错误试试这个:

CREATE TABLE [dbo].[Users] (
    [Id]       int NOT NULL AUTO_INCREMENT,
    [UserName] NCHAR (50)    NULL,
    [Email]    NCHAR (50)    NULL,
    [Password] NCHAR (50)    NULL,
    [Role]     NCHAR (15)    NULL,
    PRIMARY KEY (id)
);

Edit after your edit:编辑后编辑:

Why are you filling in a new GUID as the id parameter?为什么要填写一个新的 GUID 作为 id 参数?

Its pretty clear why you're getting these random strings as your id... If you define the id to automatically increment the number, you dont have to specify the id parameter很清楚为什么你将这些随机字符串作为你的 id ......如果你定义了 id 来自动增加数字,你就不必指定 id 参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM