none
how to establish connection between vb.net and sql server 2005 database???

    Question

  • Hello,

    Hii friends,

    I want to know how to connect sql server 2005 to vb.net windows application

    1)is there any connection string ?


    2)if so i am using windows authendication in sql server 2005 management studio so am not entering any user id and

    password to enter in sql server

    3)so what should be the userid and password for the connection string

    can any one send me a sample code that i can refar

    can any one help me with the process

     

    Tuesday, September 15, 2009 11:19 AM

Answers

  • Hello,

    Hii friends,

    I want to know how to connect sql server 2005 to vb.net windows application

    1)is there any connection string ?


    2)if so i am using windows authendication in sql server 2005 management studio so am not entering any user id and

    password to enter in sql server

    3)so what should be the userid and password for the connection string

    can any one send me a sample code that i can refar

    can any one help me with the process

     


    I dont advice you to use password, course too much problem....

    to conect to your dabase you can wizard from your right side click on data source, add new data source....just follow the step

    or by code:

    Dim conectme as string="Data Source=Localhost\SQLEXPRESS_DECK;Integrated Security=True; Initial Catalog=SalvadorCorreia"

     

    Dim connectString As SqlConnection = New SqlConnection(conectme )

     

    Try

     

    'Dim po As SqlCommand = New SqlCommand(procurame, cnn)

     

    Dim dataAdapter1 As New SqlDataAdapter(New SqlCommand("Select * From Staff", connectString))

     

    Dim ds As New DataSet("Northwind Staff")

    ds.Tables.Add(

    "Staff")

     

    Me.BindingSource1.DataSource = (ds.Tables("Staff"))

     

    Me.DataGridView1.DataSource = Me.BindingSource1

     

    add a bindingsource and a datagridview

    Finally

    connectString.Dispose()

     

    End Try

     

     

     




    Don't judge me, just Upgrade me. Thanks!
    • Marked as answer by roma_victa Thursday, September 17, 2009 11:48 AM
    Tuesday, September 15, 2009 9:37 PM

All replies

  • Dim conn As New SqlConnection
    conn.ConnectionString = "Data Source=MachineName;Initial Catalog=DatabaseName;Integrated Security=True"
    conn.Open
    "Integrated Security" is equivalent to "Windows Authentication", and no userid/password is required in this case.
    Mark the best replies as answers!
    Tuesday, September 15, 2009 1:02 PM
  • Hello,

    Hii friends,

    I want to know how to connect sql server 2005 to vb.net windows application

    1)is there any connection string ?


    2)if so i am using windows authendication in sql server 2005 management studio so am not entering any user id and

    password to enter in sql server

    3)so what should be the userid and password for the connection string

    can any one send me a sample code that i can refar

    can any one help me with the process

     


    I dont advice you to use password, course too much problem....

    to conect to your dabase you can wizard from your right side click on data source, add new data source....just follow the step

    or by code:

    Dim conectme as string="Data Source=Localhost\SQLEXPRESS_DECK;Integrated Security=True; Initial Catalog=SalvadorCorreia"

     

    Dim connectString As SqlConnection = New SqlConnection(conectme )

     

    Try

     

    'Dim po As SqlCommand = New SqlCommand(procurame, cnn)

     

    Dim dataAdapter1 As New SqlDataAdapter(New SqlCommand("Select * From Staff", connectString))

     

    Dim ds As New DataSet("Northwind Staff")

    ds.Tables.Add(

    "Staff")

     

    Me.BindingSource1.DataSource = (ds.Tables("Staff"))

     

    Me.DataGridView1.DataSource = Me.BindingSource1

     

    add a bindingsource and a datagridview

    Finally

    connectString.Dispose()

     

    End Try

     

     

     




    Don't judge me, just Upgrade me. Thanks!
    • Marked as answer by roma_victa Thursday, September 17, 2009 11:48 AM
    Tuesday, September 15, 2009 9:37 PM
  • hi

    i have done this but its showing an error when i tried to put a the data in a msgbox

    MsgBox(ds.Tables(

     

    "user").Rows(0))

    it says IndexOutOfRangeException was out of range

    There is no row at position 0

    but there is data at position 0 in the table

    is it because of the connection problem or any other..

    her is the code

     

    Dim conectme As String = "Data Source=Localhost\SQLEXPRESS;Integrated Security=True; Initial Catalog=Hr_Recruitment"

     

     

     

     

     

     

     

     

     MsgBox(ds.Tables(

    Dim conectme As String = "Data Source=Localhost\SQLEXPRESS;Integrated Security=True; Initial Catalog=Hr_Recruitment"

    Dim dataAdapter1 As New SqlDataAdapter(New SqlCommand("Select * From user",connectString)) Dim ds As New DataSet(" hr")

    ds.Tables.Add(

    "user") "user").Rows(0).Item(1))


    I think the problem is with the connection string

     

    "Data Source=Localhost\SQLEXPRESS" is it localhost or my computer name.


    "Initial Catalog=Hr_Recruitment" where name if the database

    ds.Tables.Add("user") 'user is the table


    Is there any tutorial that i can search and give the complete details of the database

    connectivity dataset and all that... 

    Thanks

    Dim connectString As SqlConnection = New SqlConnection(conectme)

    Try
    Wednesday, September 16, 2009 7:22 AM
  • Hi roma_victa,

        Welcome to MSDN forums!
        I think connect string of your program is correct.
        Would you please post all your code using </> and select VB.Net language for testing?
        You can refer to the following link for connection strings
        http://www.connectionstrings.com/
        If you want to add rows into DataTable, you can follow the following link
        http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx

    Regards
    Jeff Shan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, September 17, 2009 11:38 AM
  • Imports System.Data.SqlClient
    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim myConnection As New SqlConnection
            Dim myCommand As SqlCommand
        

            myConnection.ConnectionString = "Data Source=localhost\SQLEXPRESS;Initial Catalog=hr;User ID=sa;Password=deedee"
          
            Try
                myConnection.Open()
              
                myCommand = New SqlCommand("Select * from dbo.user", myConnection)
              
              

                Dim dataAdapter1 As New SqlDataAdapter(myCommand)
                Dim ds As New DataSet()

                dataAdapter1.Fill(ds)



                MsgBox(ds.Tables(0).Rows(0).Item(1))


              
                myConnection.Close()
            Catch ex As Exception
                MsgBox("connection error")
            End Try

        End Sub
    End Class



    The problem is here dataAdapter1.Fill(ds) as soon as it reaches here it throws the exception

    kindly advice

    Thursday, September 17, 2009 1:23 PM
  • was missing that bit:

       ds.Tables.Add(

     

    "StudentInf")
       dataAdapter1.Fill(ds.Tables(
    "StudentInf"))

    MsgBox(ds.Tables(0).Rows(0).Item(1))
    Don't judge me, just Upgrade me. Thanks!
    Thursday, September 17, 2009 9:53 PM