How can Connect to the SQL Server and Run Commands in VB.Net & C# ?

If you have difficulty to connect to the SQL Server, read this post, it can help you to understand the fundamental:
First let me to explain the basic objects:
This objects contains SQL Connection String, other objects use this object to connect to the SQL Server
This object contains SQL Commands that will be run, it can carry SQL commands or Stored procedure name.
3. SqlDataAdapter
If you want to run Commands that return data (records), you should use this object, it can bring data from SQL Server to your application

now we are familiar with the basic objects that we need to connect to the SQL Server, let start to write the function that run the SQL command for us.


Private Sub ConnectToSQLServer()
            Dim SqlConnection As New Data.SqlClient.SqlConnection(
"Integrated Security=SSPI;Persist Security Info=False;Initial 
Catalog=Diet;Data Source=.\server2008")
            Dim SQLcommand As New Data.SqlClient.SqlCommand("SELECT * FROM Foods", SqlConnection)
            Dim SqlDataAdapter As New Data.SqlClient.SqlDataAdapter(SQLcommand)
            Dim DtResult As New DataTable
        Catch ex As Exception
            Throw ex.InnerException
        End Try
    End Sub

private void ConnectToSQLServer()
    try {
        System.Data.SqlClient.SqlConnection SqlConnection = new System.Data.SqlClient.SqlConnection
("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Diet;Data Source=.\\server2008");
        System.Data.SqlClient.SqlCommand SQLcommand = 
           new System.Data.SqlClient.SqlCommand("SELECT * FROM Foods", SqlConnection);
        System.Data.SqlClient.SqlDataAdapter SqlDataAdapter =
          new System.Data.SqlClient.SqlDataAdapter(SQLcommand);
        DataTable DtResult = new DataTable();
    } catch (Exception ex) {
        throw ex.InnerException;

8 thoughts on “How can Connect to the SQL Server and Run Commands in VB.Net & C# ?”

  1. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog
    that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  2. I do consider all the ideas you have offered on your post.
    They are really convincing and will definitely work. Still,
    the posts are very brief for novices. May you please lengthen them a bit
    from subsequent time? Thank you for the post.

  3. Hi everyone, it’s my first pay a quick visit at this website, and piece of writing is genuinely fruitful for me, keep up posting such articles or reviews.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s