How to get next auto increment value from access database in c# during insert statement

前端 未结 2 507
别那么骄傲
别那么骄傲 2021-01-25 22:55

I am currently entering data from a Windows Form Application written in C#. My Database is a Microsoft Access Database. Everything is working fine accept I keep getting errors

相关标签:
2条回答
  • 2021-01-25 23:12

    Auto-number facilitates you to not bother about getting next value. AutoNumber should not be offering you any problem in insert as you are not using it field set, it gets next value automatically for insertion.

    First you Try

    "INSERT INTO MonsterDrops (MonsterName, Drop, AmountDropped) VALUES 
    ('BOOGYMAN', 'BOOGERS', '1')";
    

    Then Try this one

    "INSERT INTO MonsterDrops (MonsterName, Drop, AmountDropped) VALUES
     ('" + Monsters.SelectedCells[0].Value.ToString() + "','" + DropName + "'
    ,'" + Amount + ")"'"
    

    If you find problem in the 2nd one then you have straight away problem in the values you are getting

    Otherwise if you get problem even in the first query, then you should try this way

    Make your ID field Number instead of auto-number and try

    "INSERT INTO MonsterDrops (ID,MonsterName, Drop, AmountDropped) VALUES 
    (2222, 'BOOGYMAN', 'BOOGERS', '1')";
    

    If you get succeeded this way, then you have to read more about auto-number and you will easily fix your problem, If you get problem even with this one, I am ready to help further.

    Edit From comments: Change your Drop named field to Drop111 or something other than Drop. Because, the Drop is keyword in SQL.

    0 讨论(0)
  • 2021-01-25 23:13

    I belive you need to set autoincrement somethink like

    ALTER TABLE MonsterDrops
       ALTER COLUMN ID AUTOINCREMENT(1001,1)
    

    basically replace 1001 with your last ID and you want to increment your next insert by 1

    0 讨论(0)
提交回复
热议问题