Welcome all! Hereby I would like to present you a little prototype of
mine. This is a program showing that you can indeed write SQL commands
using C#. Try out the code below, and if you are interested in reading
further tutorials... please leave me a comment and some +rep. 
Connecting or creating a database
For a start let's run Visual Studio 2008. I also installed SQL Express which runs locally. If you want to connect remotly using TCP/IP then we can work on that, too. But I do not do that in this tutorial, part 1.
Now, before touching any database you need to do three things. For one, you need to create a database. In the Database Explorer click-right and click Add Connection. There you select a database provider (SqlClient is default) and a database file. I selected a new database file, to create a new database. Go ahead and create your own database, I will wait.
For two, you need to create a table. Again in the Database Explorer select the database, then Tables folder, then right-click and click Add New Table. Now go ahead and put few columns there, along with their types. I used types int and ntext. Better avoid other types, like DateTime . Then save the table, pick your own name for it. Go ahead.
For three, you need to make a connection string. This time use the Data Sources panel and right-click, then click Add New Data Source. The wizard is pretty straightforward. Select a Database. It should propose you the database file, that you selected earlier.
Warning: It will ask you do you want to copy the database into your project. If you click yes then it will restore the database at every run. I strongly suggest to click No. Otherwise you will loose your records all too often.
Next page will ask to save a connection string. You are going to need it in the code. Last page will ask you to import tables. I suggest you just import them all and sort it out later.
Writing SQL command: INSERT into table
Here we are. The precious moment of truth. Let us insert a nice record into our new table. Here is a simple piece of code that does that. Notice that you are going to include an import directive, use two classes, and that is about it.
The namespace System.Data.SqlClient contains the two classes, SqlConnection and SqlCommand that will do the whole work. As for the SQL language itself, there is some nice MSDN documentation for that.
Connecting or creating a database
For a start let's run Visual Studio 2008. I also installed SQL Express which runs locally. If you want to connect remotly using TCP/IP then we can work on that, too. But I do not do that in this tutorial, part 1.
Now, before touching any database you need to do three things. For one, you need to create a database. In the Database Explorer click-right and click Add Connection. There you select a database provider (SqlClient is default) and a database file. I selected a new database file, to create a new database. Go ahead and create your own database, I will wait.
For two, you need to create a table. Again in the Database Explorer select the database, then Tables folder, then right-click and click Add New Table. Now go ahead and put few columns there, along with their types. I used types int and ntext. Better avoid other types, like DateTime . Then save the table, pick your own name for it. Go ahead.
For three, you need to make a connection string. This time use the Data Sources panel and right-click, then click Add New Data Source. The wizard is pretty straightforward. Select a Database. It should propose you the database file, that you selected earlier.
Warning: It will ask you do you want to copy the database into your project. If you click yes then it will restore the database at every run. I strongly suggest to click No. Otherwise you will loose your records all too often.
Next page will ask to save a connection string. You are going to need it in the code. Last page will ask you to import tables. I suggest you just import them all and sort it out later.
Writing SQL command: INSERT into table
Here we are. The precious moment of truth. Let us insert a nice record into our new table. Here is a simple piece of code that does that. Notice that you are going to include an import directive, use two classes, and that is about it.
The namespace System.Data.SqlClient contains the two classes, SqlConnection and SqlCommand that will do the whole work. As for the SQL language itself, there is some nice MSDN documentation for that.
using System.Data.SqlClient;
SqlConnection connection1 = new SqlConnection( Properties.Settings.Default.some_numbersConnectionString); SqlCommand insertCommand = new SqlCommand( "INSERT into NumbersTable values (128, 'as in 128 bits equal 16 bytes')", connection1); connection1.Open(); insertCommand.ExecuteNonQuery(); connection1.Close(); MessageBox.Show("Record inserted. Please check your table data. :)");
0 Comments
Good day precious one, We love you more than anything.