9 Replies - 20223 Views - Last Post: 08 February 2012 - 03:39 PM Rate Topic: -----

#1 newbownerton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-February 12

Connecting to SQL database

Posted 08 February 2012 - 12:05 PM

This is my first post so before I move on to my question I just want to say HELLO to everyone :)

I am a wannabe programmer but quite new, started with VB.net about 1 month ago.

I write on this forum because It's the first time that I'm actually stuck. The problem is I cannot find a way to connect to SQL server database I have created and saved earlier on my HD using visual express 2010. I want to find a way to connect using code, not by click and drop. I just hope you won't find my issue too trivial to even answer it :)

The project is very simple, it just consists of a button that should connect to the db on click. The code is as follows:

Imports System.Data
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 constring As String
        constring = "Data Source=C:\Users\W\Desktop\Visual Basic Projekty\db1.mdf;"

        Dim conn As New SqlConnection(constring)
        conn.Open()

        MsgBox("Connected")


    End Sub

End Class


Please give me some hints, I will really appreciate it. Thank you :)

Is This A Good Question/Topic? 0
  • +

Replies To: Connecting to SQL database

#2 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Connecting to SQL database

Posted 08 February 2012 - 12:16 PM

hey, welcome to DIC!

soo what's the problem? are you getting an error? btw what's the server version you're using?

And I have to ask you, if you created a database on the server, why are you trying to connect it its mdf file? you can just connect to the database itself straight up. all you have to change is the connection string.

This post has been edited by nK0de: 08 February 2012 - 12:18 PM

Was This Post Helpful? 1
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14482
  • View blog
  • Posts: 58,045
  • Joined: 12-June 08

Re: Connecting to SQL database

Posted 08 February 2012 - 12:21 PM

Have you had a chance to flip through the database tutorials? Two come to mind that might help you out.

OleDb Basics in VB.Net Rate Topic

A Really Simple Database Create a Database using Access & VB.net Express 2008
Was This Post Helpful? 0
  • +
  • -

#4 newbownerton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-February 12

Re: Connecting to SQL database

Posted 08 February 2012 - 12:37 PM

View PostnK0de, on 08 February 2012 - 12:16 PM, said:

hey, welcome to DIC!


Thank you :)

Quote

soo what's the problem? are you getting an error? btw what's the server version you're using?


My bad, forgot to mention what happens... When I press the button the program just freezes up.

The server version... server version... Umm, I created the database file like this:

Data ==> Add New Data Source ==> Database ==> Dataset ==> New Connection ==>

Posted Image

==> Use Windows authentication

Quote

And I have to ask you, if you created a database on the server, why are you trying to connect it its mdf file? you can just connect to the database itself straight up. all you have to change is the connection string.


After completing above steps there are 2 new files on my HD: "db1.mdf" and "db1_log.LDF". I thought db1.mdf is a database file ;/

What would you change in the connection string to make it work ?

PS. Sorry for being such a noob lol

View Postmodi123_1, on 08 February 2012 - 12:21 PM, said:

Have you had a chance to flip through the database tutorials? Two come to mind that might help you out.

OleDb Basics in VB.Net Rate Topic

A Really Simple Database Create a Database using Access & VB.net Express 2008


Thanks for your reply but I think those tutorials are referring to Access databases. I want to learn how to connect to SQL Server database not JET.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14482
  • View blog
  • Posts: 58,045
  • Joined: 12-June 08

Re: Connecting to SQL database

Posted 08 February 2012 - 12:44 PM

Yes but the process is the same. If you are having strictly issues with your connection string - then I assume you were looking up the format on http://connectionstrings.com . If you were interested in how the flow of opening/creating a connection to a database (be it access or sql) then those tutorials would help. The knowledge is transferable (if not the same) with the minor niggling over the connection string.
Was This Post Helpful? 1
  • +
  • -

#6 newbownerton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-February 12

Re: Connecting to SQL database

Posted 08 February 2012 - 12:53 PM

View Postmodi123_1, on 08 February 2012 - 12:44 PM, said:

Yes but the process is the same. If you are having strictly issues with your connection string - then I assume you were looking up the format on http://connectionstrings.com . If you were interested in how the flow of opening/creating a connection to a database (be it access or sql) then those tutorials would help. The knowledge is transferable (if not the same) with the minor niggling over the connection string.


The problem is I already made a tutorial on how to connect to an Access database and it succeeded, but there is no way I can do it for SQL Server type. I'm just lost. I checked up with http://connectionstrings.com but nothing comes to my mind, really. No point of attachment. Tried something like this, am i getting closer?

Imports System.Data
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 constring As String
        constring = "Server=.\SQLExpress;AttachDbFilename=|DataDirectory|db1.mdf; Database=db1;Trusted_Connection=Yes;"

        Dim conn As New SqlConnection(constring)
        conn.Open()

        MsgBox("Connected")


    End Sub

End Class


This time it crashes on "conn.Open()" line. ;/
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14482
  • View blog
  • Posts: 58,045
  • Joined: 12-June 08

Re: Connecting to SQL database

Posted 08 February 2012 - 12:59 PM

I would assume "|DataDirectory|" means the path to your directory.
Was This Post Helpful? 0
  • +
  • -

#8 newbownerton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-February 12

Re: Connecting to SQL database

Posted 08 February 2012 - 01:04 PM

View Postmodi123_1, on 08 February 2012 - 12:59 PM, said:

I would assume "|DataDirectory|" means the path to your directory.

OMG, so ashamed of myself ;/

Changed the code:

Imports System.Data
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 constring As String
        constring = "Server=.\SQLExpress;AttachDbFilename=C:\Users\W\Desktop\Visual Basic Projekty\db\dbconnect\db1.mdf; Database=db1;Trusted_Connection=Yes;"

        Dim conn As New SqlConnection(constring)
        conn.Open()

        MsgBox("Connected")


    End Sub

End Class


But still the same error ;/
Was This Post Helpful? 0
  • +
  • -

#9 newbownerton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-February 12

Re: Connecting to SQL database

Posted 08 February 2012 - 02:43 PM

OMG, i finally made it!

this helped me out "http://connectionstrings.com". Upvoted. Thanks !
Was This Post Helpful? 0
  • +
  • -

#10 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Connecting to SQL database

Posted 08 February 2012 - 03:39 PM

I was just about to say that is the best place to get your strings from if you have a hard time creating them, but also if you use the designer to connect to a database, it will have a drop down tab mid way through setup that gives you the connection string that's usually what I use when I have difficulty.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1