Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How I can connect VB.net to SQL Server 2005 Express?

How I can connect VB.net to SQL Server 2005 Express?

Scheduled Pinned Locked Moved Visual Basic
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ATC
    wrote on last edited by
    #1

    I am very new for VB.net (Visual Studio 2005) and also very new for SQL Server 2005 Express I do have a Trial1.mdf install in Server Local c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Trial1.mdf I tried to connect/open/close as the following codes: Imports System.Data.SqlClient Public Class Form1 Dim sqlConnection As New SqlConnection("server=localhost;uid=NULL;pwd=NULL;database= c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Trial1.mdf") sqlConnection.Open() sqlConnection.Close() End Class But it fail to run (even re-build no-error), can someone tell me what should I do to use my Trial1.mdf correctly? Thanks for any help

    H 1 Reply Last reply
    0
    • A ATC

      I am very new for VB.net (Visual Studio 2005) and also very new for SQL Server 2005 Express I do have a Trial1.mdf install in Server Local c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Trial1.mdf I tried to connect/open/close as the following codes: Imports System.Data.SqlClient Public Class Form1 Dim sqlConnection As New SqlConnection("server=localhost;uid=NULL;pwd=NULL;database= c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Trial1.mdf") sqlConnection.Open() sqlConnection.Close() End Class But it fail to run (even re-build no-error), can someone tell me what should I do to use my Trial1.mdf correctly? Thanks for any help

      H Offline
      H Offline
      Harini N K
      wrote on last edited by
      #2

      Hi For connecting to database, you need to provide UID, password and database. The .MDF file can be attached to the SQL Server. For attaching the database, Check this link Attaching Database[^] After attaching, your database name will be the datasource in your connection string. Hope this helps.

      Harini

      A 1 Reply Last reply
      0
      • H Harini N K

        Hi For connecting to database, you need to provide UID, password and database. The .MDF file can be attached to the SQL Server. For attaching the database, Check this link Attaching Database[^] After attaching, your database name will be the datasource in your connection string. Hope this helps.

        Harini

        A Offline
        A Offline
        ATC
        wrote on last edited by
        #3

        I did try as you said and it improves my issue, however I still can not open and use my trial1.mdf. Here my update codes: Imports System.Data.SqlClient Public Class Form1 'Link database Dim DB_Location = "c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Trial1.mdf" Dim MyConnection = "Server=localhost; DataBase= " & DB_Location & "Integrated Security=SSPI" Dim sqlConnection As New SqlConnection(MyConnection) 'Use database Dim strSQL As String = "SELECT FROM Trial1 WHERE col2 = 'trial' " Dim cmd As New SqlCommand(strSQL, MyConnection) sqlConnection.Open() ' ----> error cmd.ExecuteNonQuery() ' ----> error sqlConnection.Close() ' ----> error End Class At the last 3 line of codes I got the errors ... can you help me to correct it? Thanks:(

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups