如何在visual c#2010中添加自动编号?使用ms Access?

如何在visual c#2010中添加自动编号?使用ms Access?,c#,ms-access,C#,Ms Access,我正在使用自动编号,但它对我不起作用。我想在我的学生ID号中自动编号 OleDbCommand system = new OleDbCommand(); system.CommandType = CommandType.Text; system.CommandText = "insert into Student(ID, Lastname, Middlename, Firstname, Address, DateofBirth, Birthplace, Contact_number, emai

我正在使用自动编号,但它对我不起作用。我想在我的学生ID号中自动编号

OleDbCommand system = new OleDbCommand();
system.CommandType = CommandType.Text;
system.CommandText = 
"insert into Student(ID, Lastname, Middlename, Firstname, Address, DateofBirth, Birthplace, Contact_number, emailaddress, guardian_name, Guardian_contact) values ('" + txtStudentIDnumber.Text + "','" + txtlastname.Text + "','" + txtfirstname.Text + "','" + 
txtmiddlename.Text + "','" + txtaddress.Text + "','" + txtdateofbirth.Text + "','" + txtbirthplace.Text + "','" + txtcontactnumber.Text + "','" + txtemailaddress.Text + "','" + txtGuardianname.Text + "','" + txtguardiancontact.Text + "')";
system.Connection = mydatabase;



if (MessageBox.Show("Save data?", "Confirm Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)

如果没有数据库模式或错误消息,很难确定问题。但是,问题可能是因为您试图在
ID
列中插入一个值,而该列可能启用了自动编号(也称为计数器)。更改:

system.CommandText = "insert into Student(ID, Lastname, ..."; // And so on


还考虑将查询更改为参数化查询(如所提到的),而不是使用级联来避免SQL注入和逃避问题。

< P>不存在数据库模式或错误消息,很难确定问题。但是,问题可能是因为您试图在
ID
列中插入一个值,而该列可能启用了自动编号(也称为计数器)。更改:

system.CommandText = "insert into Student(ID, Lastname, ..."; // And so on


还考虑将查询更改为参数化查询(如所提到的),而不是使用连接来避免SQL注入和逃避问题。

您的ID列应该被设置为身份(在数据库中),然后您应该从INSERT中省略它。

更新

我怀疑您的StudentDNumber是一个实际的州颁发的ID号,您要查找的是一个身份字段

您需要使用用于创建表的表设计器或脚本向表中添加标识列

CREATE TABLE Student(
   ID int identity, 
   StudentIdNo varchar(10), 
   Lastname varchar(10), 
   Firstname varchar(10), 
   Middlename varchar(10), 
   CONSTRAINT AutoIncrementTest_PrimaryKey PRIMARY KEY (ID)
)
这将是insert语句的格式,注意没有ID字段

…在您的情况下,在添加标识字段后


您的ID列应该设置为一个标识(在数据库中),然后您应该在插入中忽略它

更新

我怀疑您的StudentDNumber是一个实际的州颁发的ID号,您要查找的是一个身份字段

您需要使用用于创建表的表设计器或脚本向表中添加标识列

CREATE TABLE Student(
   ID int identity, 
   StudentIdNo varchar(10), 
   Lastname varchar(10), 
   Firstname varchar(10), 
   Middlename varchar(10), 
   CONSTRAINT AutoIncrementTest_PrimaryKey PRIMARY KEY (ID)
)
这将是insert语句的格式,注意没有ID字段

…在您的情况下,在添加标识字段后


首先,您应该像下面这样指定标识列:

然后您的代码:

 OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\db.accdb");
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = string.Format("insert into Student(LastName,...) values('{0}',...)",txtLastName.Text.Trim(),...);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

首先,您应该像下面这样指定标识列:

然后您的代码:

 OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\db.accdb");
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = string.Format("insert into Student(LastName,...) values('{0}',...)",txtLastName.Text.Trim(),...);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

如何插入行?(请显示代码)您很可能正在为“自动编号”列提供值。如果不是这样的话,那么给我们看一下你的表格描述,以及insert语句。我不知道该怎么办。对不起,我不知道。我也注意到了这一点。您的文本框顺序不正确。FirstName和MiddleName需要交换。如何插入行?(请显示代码)您很可能正在为“自动编号”列提供值。如果不是这样的话,那么给我们看一下你的表格描述,以及insert语句。我不知道该怎么办。对不起,我不知道。我也注意到了这一点。您的文本框顺序不正确。FirstName和MiddleName需要在运行系统时交换,StudentID的文本框没有编号。我该怎么办?@ansie23要么生成一个唯一的编号并将其插入ID字段(假设它是主键),要么按照Sirwan的指示将该字段设置为自动编号。当我运行系统时,StudentID的文本框没有编号。我该怎么办?@ansie23要么生成一个唯一的数字并将其插入ID字段(假设它是主键),要么按照Sirwan的指示将该字段设置为自动编号。