C# 插入自动编号字段文本框的语句(Access)

C# 插入自动编号字段文本框的语句(Access),c#,ms-access,auto-increment,windows-forms-designer,autonumber,C#,Ms Access,Auto Increment,Windows Forms Designer,Autonumber,我有一个Windows窗体,它有以下文本框,并显示在gridview中。应用程序使用C#连接到Access数据库 公司ID(自动编号) 公司名称(短文本) 公司类型(短文本) 如何生成自动编号字段以使用INSERT语句更新自身 e、 g.,C001,C002,C003,C004 OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Shrenik_Sal

我有一个Windows窗体,它有以下文本框,并显示在gridview中。应用程序使用C#连接到Access数据库

公司ID(自动编号)
公司名称(短文本)
公司类型(短文本)

如何生成自动编号字段以使用INSERT语句更新自身

e、 g.,C001,C002,C003,C004

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Shrenik_Salguna\Desktop\final.accdb;
        Persist Security Info=False;");
        con.Open();

        OleDbCommand cmd = new OleDbCommand(@"INSERT INTO info
                     ([Name of Company], [Type of Company]) VALUES('"+textBox1.Text+"','" + textBox2.Text + ")", con);
        cmd.ExecuteNonQuery();
        con.Close();

如果[Companyid]是Access表中的一个
自动编号
字段,则在INSERT语句中不包括该字段,因为Access数据库引擎会为您处理该字段

可以想象,您可以创建自己的“自动递增”字段,其中包含“C001”、“C002”等,但如果您已经有一个真正的
AutoNumber
字段,那么您为什么还要费事呢?表中的每一行都有一个唯一的列,如果您想派生一个类似“Cnnn”的标识符,那么您可以在C#中轻松实现,只需使用与此VBA表达式等效的东西:

"C" & Format([Companyid], "000")

下面是我如何创建一个带有自动编号字段的表:

        ADOX.Catalog cat = new ADOX.Catalog();
        ADOX.Table table = new ADOX.Table();
        ADOX.Key tableKey = new Key();
        ADOX.Column col = new Column();

        String SecurityDBConnection = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\{1};", value, SecurityDBName);

        // Define column with AutoIncrement features
        col.Name = "ID";
        col.Type = ADOX.DataTypeEnum.adInteger;


        // Define security table
        table.Name = "Security";
        table.Columns.Append(col);    // default data type is text[255]
        table.Columns.Append("Username", ADOX.DataTypeEnum.adVarWChar, 255);
        table.Columns.Append("Password", ADOX.DataTypeEnum.adVarWChar, 255);
        table.Columns.Append("Engineer", ADOX.DataTypeEnum.adBoolean);
        table.Columns.Append("Default", ADOX.DataTypeEnum.adBoolean);

        tableKey.Name = "Primary Key";
        tableKey.Columns.Append("ID");
        tableKey.Type = KeyTypeEnum.adKeyPrimary;

        // Add security table to database
        cat.Create(SecurityDBConnection);

        // Must create database file before applying autonumber to column
        col.ParentCatalog = cat;
        col.Properties["AutoIncrement"].Value = true;

        cat.Tables.Append(table);

        // Now, try to connect to cfg file to verify that it was created successfully
        ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
        if (con != null) con.Close();
下面是将记录插入带有自动编号字段的表中的代码。注意,自动编号字段不是insert语句中指定的,字段名用括号括起来

        public void WriteRecord(String sUsername, String sPassword, Boolean boEngineerRole, Boolean boDefaultUser)
       {
        String InsertQry = "Insert into Security([Username], [Password], [Engineer], [Default]) "
            + "values(@UserName, @Password, @Engineer, @Default)";
        using (OleDbConnection connection = new OleDbConnection(SecurityDBConnection))
        {
           using (OleDbCommand command = new OleDbCommand(InsertQry, connection))
           {
               command.CommandType = CommandType.Text;
               command.Parameters.AddWithValue("@UserName", sUsername);
               command.Parameters.AddWithValue("@Password", sPassword);
               command.Parameters.AddWithValue("@Engineer", boEngineerRole);
               command.Parameters.AddWithValue("@DefaultUser", boDefaultUser);
               connection.Open();
               command.ExecuteNonQuery();
           }
        }
    }