4 Replies - 14809 Views - Last Post: 27 January 2012 - 07:11 AM

#1 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Make Connection between MS Sql Server and Vb.net

Posted 24 January 2012 - 02:49 PM

I'm using vb.net 2008
I create a project and save it
I'm new on using Ms sql server database. I create a DB in Ms Sql server and called MyDBs as it show in a pic below
When I was using Db Ms access with vb.net. I put the DB Ms access file in the Folder.. bin\Debug..of my project and then use this connection to connect between Db and vb.net
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source =" & Application.StartupPath & "\MyDB.mdb")

So what I have to do to make same thing with Ms Sql Server?

Attached image(s)

  • Attached Image

This post has been edited by Mangore: 24 January 2012 - 02:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Make Connection between MS Sql Server and Vb.net

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Make Connection between MS Sql Server and Vb.net

Posted 25 January 2012 - 03:02 PM

Short answer, NO!

When working with SQLServer, don't worry about the files and their location because you connect to a server (not to a file like with Access). The only thing you should worry is that they should be in a safe place(another partition than the one OS is installed). In case something happens to the OS, a simple reattache will put the database online.

In vb.net, don't use OleDb to connect to a SQLServer. You have SQLClient.
And the connection string to use is this. As you will see, you don't have to specify a path, but a server(machine name or ip - in your case Nader-PC or . (dot = localhost)) and the database.

This post has been edited by Ionut: 25 January 2012 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Make Connection between MS Sql Server and Vb.net

Posted 26 January 2012 - 10:49 AM

My DataBase as it show in the Pic in previous post is : MyDBs
Is thsi true connection to it :
Imports System.Data.SqlClient

 Dim MyConnection As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & "Initial Catalog=MyDBs")

Was This Post Helpful? 0
  • +
  • -

#4 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Make Connection between MS Sql Server and Vb.net

Posted 27 January 2012 - 01:22 AM

Just use as one string, you don't need & sign

Imports System.Data.SqlClient

 Dim MyConnection As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=MyDBs"



Anyway, it should work.

This post has been edited by Ionut: 27 January 2012 - 01:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Make Connection between MS Sql Server and Vb.net

Posted 27 January 2012 - 07:11 AM

I treid this code to load data from DB
 Dim MyConnection As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=MyDBs")

    Public Sub Load_DBmsql()
        Dim sql As String = " Select Name,Tel,Address from Table1 "
        Dim dp As New SqlDataAdapter(sql, MyConnection)

        Dim Table1 As New DataTable
        dp.Fill(Table1)
        Form1.DataGridView1.DataSource = Table1
    End Sub


It showed me this error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

This post has been edited by Mangore: 27 January 2012 - 07:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1