0

다음 텍스트 상자가있는 Windows 양식이 있고 gridview에 표시됩니다. 응용 프로그램은 C#을 사용하여 Access 데이터베이스에 연결됩니다.일련 번호 필드 텍스트 상자 (액세스)에 대한 INSERT 문

회사 ID (일련 번호)
회사 명 (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

[회사 ID]를 Access 테이블에 AutoNumber 필드가 당신이 당신의 INSERT 문에서 해당 필드를 포함하지 않는 경우 때문에 Access 데이터베이스 엔진이이를 처리합니다.

당신은 생각할 등, "C002", "C001"를 포함 자신의 "자동 증가"필드를 만들 수 있지만, 당신은 이미 다음 진정한 AutoNumber 필드가 있다면 왜 귀찮게 것? 이미 테이블에 각 행에 대한 고유 열이, 당신은 다음 "Cnnn"와 같은 식별자를 도출하기를 원한다면 당신은 쉽게 그냥이 VBA 표현에 상응하는 뭔가를 사용하여 C#으로 그렇게 할 수 있습니다 :

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