2013-03-22 62 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

如果你不想使用标识列,你可以使用查询来查找当前新行最高的数字,然后添加一个。建议不要这样做,因为您可能有两个客户端同时执行此操作,所以在尝试写入数据库时​​会失败。

相关问题