I need a sample C# (console application) code witch connects to an SQL Server Express database
and inserts a few variables into a table “laptops”
- SQL Server Express is @ localhost
- user name is database
- and password is testdatabase
What is the proper way to do that ?
Basic ADO.NET 101:
Step 1: setting up a connection
You need to know the connection string to your database. Check out http://www.connectionstrings.com for a ton of examples.
In your case, you say it’s a local SQL Server Express instance – but unfortunately, you didn’t mention what your database is called….. your connection string will be something like:
Step 2: setting up a command
You can have various commands – to select data, to delete that, or to insert data. Whatever you do – I would recommend to always use parametrized queries to avoid SQL injection.
So your code here would look something like: