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:
1.SqlConnection
This objects contains SQL Connection String, other objects use this object to connect to the SQL Server
2.SqlCommand
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.

[VB.NET]

Private Sub ConnectToSQLServer()
Try
            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
            SqlConnection.Open()
            SqlDataAdapter.Fill(DtResult)
            SqlConnection.Close()
        Catch ex As Exception
            Throw ex.InnerException
        End Try
    End Sub

[C#]
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();
        SqlConnection.Open();
        SqlDataAdapter.Fill(DtResult);
        SqlConnection.Close();
    } catch (Exception ex) {
        throw ex.InnerException;
    }
}


Categories: .Net

8 replies

  1. Tarot celtique tirage tarot en direct

  2. This information is worth everyone’s attention. Where can I find out more?

  3. There is definately a great deal to know about this issue.
    I love all of the points you’ve made.

  4. You ought to be a part of a contest for one of the greatest websites online.
    I am going to highly recommend this web site!

  5. 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.

  6. 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.

  7. Way cool! Some extremely valid points! I appreciate you penning this write-up plus the rest of the website is also very good.

  8. 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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: