3 Replies - 862 Views - Last Post: 25 January 2013 - 10:03 AM Rate Topic: -----

#1 voljin099   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-January 13

Sql connection

Posted 25 January 2013 - 12:54 AM

I have a problem with checking if my database is connected to my project in vb.net, could someone help me? here is my codes, I would appreciate your help, this program that I'm currently working on is for my thesis BTW.

Imports System.Data.SqlClient
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sqlconn As New SqlConnection
        sqlconn.ConnectionString = "server = (local);Database=OJT;integrated security=true"
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Connection to Sql Server Failed!", MessageBoxButtons.OK)
        End Try
        Me.Text = "You are successfully connected to Sql Server"
    End Sub
End Class

This post has been edited by Atli: 25 January 2013 - 01:41 AM
Reason for edit:: Use [code] tags when posting code.

Is This A Good Question/Topic? 0
  • +

Replies To: Sql connection

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Sql connection

Posted 25 January 2013 - 01:50 AM

Line 12, where you say the connection has been successful, is misleading. That line will always execute, no matter what happened in the Try-Catch block. Even if the connection fails and it executes the Catch block, it will still go on to execute line 12 afterwards.

If you want line 12 to execute only if the connection opens successfully, then you need to move that line into the Try block, after the Open() call. That way it will be skipped if the open fails and the code jumps to the Catch block.

You should also look at the SqlConnection.State property in regards to checking whether the connection is open.
Was This Post Helpful? 0
  • +
  • -

#3 jgferguson   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 11-July 12

Re: Sql connection

Posted 25 January 2013 - 09:58 AM


It would help us greatly if you could explain what the problem you have is.

I assume you are getting the message 'connection to SQL Server failed' which means there is a problem with your connection string. The following website will help point you in the right direction: http://www.connectionstrings.com/

As we don't know what flavour of database you are attempting to connect to we can be of little further help.

As a pointer perhaps, on my local machine I run SQL Server Express 2008 R2 - the connection string for my applications is 'Server=LocalHost\SQLEXPRESS; Database=MyDB; ...'

Sorry can't be of more help - connectionstrings.com is a very useful site and will be a good place to start.

Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Sql connection

Posted 25 January 2013 - 10:03 AM

one thing I would like to add to this is, if you add your database to the SERVER explorer in VB.net, you can click on the database and a valid connectionstring should show in the properties for that database. At which point you can copy and past it into your connectionstring code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1