2013-03-22 63 views
0

我只是想增加ID與限制等同於其他表插入vb.net如何遞增的ID,如果我按一下按鈕

我對此

沒有問題,唯一的問題是,我上傾斜的id碼遞增直到限位

id列2

1 ----------- 0

2 ----------- 5

3 ----------- 0

等等,像這樣,通過cliking插入按鈕,ID應該由一個直到極限增加一個

任何人都可以請幫助我

RecordDate.Text = MyFormat 

    Dim antinull As Integer = Format(0) 
    BadBeg.Text = antinull 
    WarePcs.Text = antinull 
    CustPcs.Text = antinull 
    Returned.Text = antinull 
    BadEnd.Text = antinull 

    Try 
     Str = "insert into BadWarehouseInv values(" 
     Str += Id.Text.Trim() 
     Str += "," 
     Str += """" & RecordDate.Text.Trim() & """" 
     Str += "," 
     Str += """" & BadBeg.Text.Trim() & """" 
     Str += "," 
     Str += WarePcs.Text.Trim() 
     Str += "," 
     Str += CustPcs.Text.Trim() 
     Str += "," 
     Str += Returned.Text.Trim() 
     Str += "," 
     Str += """" & BadEnd.Text.Trim() & """" 
     Str += ")" 
     Con.Open() 
     Cmd = New OleDbCommand(Str, Con) 
     Cmd.ExecuteNonQuery() 
     Dst.Clear() 
     Dad = New OleDbDataAdapter("SELECT * FROM BadWarehouseInv ORDER BY Id", Con) 
     Dad.Fill(Dst, "stock") 

     Con.Close() 
    Catch ex As Exception 
     MessageBox.Show("Could Not Insert Record!!!") 
     MsgBox(ex.Message & " - " & ex.Source) 
     Con.Close() 
+0

請考慮[String.Format()](http://msdn.microsoft.com/en-us/library/fht0f5be%28v=vs.80%29.aspx):http://stackoverflow.com/問題/ 7054069 /插入 - 形成 - 字符在字符串格式 – tjameson 2013-03-22 10:15:36

+1

比這更好,請考慮參數化查詢! http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i – 2013-03-22 10:16:18

+0

@tjameson'String.Format'可能更具可讀性,但同樣易受影響注射,你應該推薦使用'SqlParameter's來代替。 – 2013-03-22 10:17:19

回答

0

我不知道我理解你的問題,但是: -

通常當你被寫入數據庫,你有一列是一個Identity type並在表或唯一標識符。這會在添加新行時自動遞增。

所以,當你在表中創建你讓這個數據庫引擎的擔心,然後用a technique to read this value back

如果你不想使用標識列,你可以使用查詢來查找當前新行最高的數字,然後添加一個。建議不要這樣做,因爲您可能有兩個客戶端同時執行此操作,所以在嘗試寫入數據庫時​​會失敗。

相關問題