Querying data using .net (VB/C#)

First thing is to get the connection string and your database name (catalog in mssql).

Then, here is the VB code to get you going all the way of getting a record:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myconnection = New SqlConnection(GetConnectionString)
        myconnection.Open()
        Dim command As New SqlClient.SqlCommand("select  first_name from users_tbl")
        command.Connection=myconnection
        Dim read As SqlClient.SqlDataReader = command.ExecuteReader()
        read.Read()
        Dim name As String = read.GetString(0)
        TextBox1.Text = name

    End Sub

I put this in a button handler where the project only has this button and a text box for an output.

 

 

Connection string to localhost with mssql express

This one is a short one, but I’ve been asked this a lot, so here it is:

How to connect to a mssql server usually the one installed with visual studio 2010/2008.

The shortest answer:

"Data Source=.\SQLEXPRESS;Initial Catalog=[your database name];Integrated Security=True;Pooling=False"

I’ve got this from many users who tried “localhost” “.” or even nothing at all. True, there were some times that actually worked, But not in the recent versions of visual studio. Also, creating a database is now being done through the server explorer:

And you will obviously still going to need the connection string.

*You might want to check this for connection code.