0

I have a database with a bunch of stuff in it, and right now I'm reading in data, doing some processing on it, and then sticking it in a new database. My code generates this string:

query_string = "INSERT INTO OrgPhrase (EXACT_PHRASE,Org_ID) VALUES (HELLO,123)"

Then it's used this way:

Dim InsertCmd = New System.Data.OleDb.OleDbCommand(query_string, connection)
InsertCmd.ExecuteNonQuery()

The associated database (OLEdb connection) exists and opens fine, with all the tables and columns it's trying to work with already existing. The error message I get is "No value given for one or more required parameters"

Am I missing something? Did I spell something wrong? I don't have a ton of experience with database work, but I've never had this trouble inserting before.

2 Answers 2

3

I believe the query should be

query_string = "INSERT INTO OrgPhrase (EXACT_PHRASE,Org_ID) VALUES ('HELLO',123)"

Also, it may happen that the table has more than 2 columns that are NOT NUll and the values to them are required.

Sign up to request clarification or add additional context in comments.

2 Comments

Could be query_string = "INSERT INTO OrgPhrase (EXACT_PHRASE,Org_ID) VALUES ('HELLO','123')" as well, depending on whether Org_Id is a string.
And that fixed it (with some new problems now for me to worry about). For the record, they're both strings.
2

Consider parameterizing the query string. There are a couple of reasons for this. First, you can pass in the values without having to worry about whether or not you need single quotes. Second, you prevent SQL injection.

query_string = "INSERT INTO OrgPhrase (EXACT_PHRASE,Org_ID) VALUES (@ExactPhrase,@OrgId)" 

You then create parametes based on the parameter names in the string. Unless, of course, your query string is always the same values, but that sounds a bit too hardcoded to be good.

1 Comment

It's not for use in anything that would be open to injection. I'm building a database from old values to do some new number crunching on them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.