问题
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 for duplicating my Primary Key value. I have been digging around the internet for a while and haven't found much pertaining to my issue.
My current table setup is below:
Field Name Data
-------------------
ID AutoNumber
MonsterName Text
Drop Text
AmountDropped Text
And my current SQL Query is below:
"INSERT INTO MonsterDrops (MonsterName, Drop, AmountDropped) VALUES ('" + Monsters.SelectedCells[0].Value.ToString() + "','" + DropName + "'," + Amount + ")"
This gives me the error: "Syntax Error in INSERT INTO statement"
I am executing the query with this function:
private void executeNonQuery(string query)
{
//create a new connection and pass in config settings for it
OleDbConnection db = new OleDbConnection(Properties.Settings.Default.DFUWDBConnectionString);
try
{
//Open the DB
db.Open();
try
{
//create a new command object
OleDbCommand command = new OleDbCommand();
//set the command to be your query
command.CommandText = query;
//set the DB for the query to be performed on
command.Connection = db;
//execute the insert
command.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
db.Close();
}
}
I read that you don't have to include the AutoNumber field if Identity is set to true, not sure if mine is set that way or not. How can I get the AutoNumber function to work while running a SQL Query???
Thanks!
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/14017526/how-to-get-next-auto-increment-value-from-access-database-in-c-sharp-during-inse