0

我有一個Windows窗體,它具有以下文本框,並顯示在gridview中。應用程序使用C#連接到Access數據庫。自動編號字段文本框的INSERT語句(訪問)

Companyid(自動編號)
公司名稱(shorttext)
TypeofCompany(shorttext)

我如何生成一個自動編號字段將自身與INSERT語句更新?

例如,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(); 

回答

2

如果[Companyid]是Access表的AutoNumber場,那麼你不要在INSERT語句字段,因爲Access數據庫引擎會爲您提供幫助。

你可以想見,創建自己「自增」,載「C001」,「C002」等領域,但如果你已經有了一個真正的AutoNumber場,那麼你爲什麼要這麼做呢?你已經有上表中每一行的唯一列,如果你想獲得像「Cnnn」的標識符,那麼你可以很容易地做到這一點在C#中通過只是使用的東西相當於這個VBA表達式:

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

這裏是我創建的表與自動編號領域:

 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(); 

這裏是一個插入一條記錄到表與自動編號字段中的代碼。請注意,autoNumber字段是NOT在插入語句中指定,並且字段名稱被括起來。

 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(); 
      } 
     } 
    }