Skip to content
Advertisement

Executing a SQL query with C#

I would like to add some information to my database. I searched for some tutorials, but none of them work.

NonQuery can do what he needs to do, because the messagebox returns “Success” (1). But it does not update my database. If I put the same query to “Add New Query”, directly to my database, it works.

Can someone help me?

My class code at the moment:

Edit:

And this is the code for my buttons etc:

Advertisement

Answer

The simplest way to insert into a SQL Server database:

As long as commandText is a working query, it should insert a row. It would be better to use parameters for your values instead of hard coding them like I did here – that avoids SQL injection attacks and other potential problems. You can search Google for that (or the question you are asking now) and find tons of resources to help you.

If you need more specific help, post details such as what is actually happening when you try to run your code – are you getting an exception?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement