Home Forums Programming Connecting Connecting to SQL Server from a Visual Studio 2012 Express Class RE: Connecting to SQL Server from a Visual Studio 2012 Express Class

  • here's a basic example for connecting, does this help?

    'form a command that waits longer than 30 seconds for testing

    Dim sqlcmd As String = "WaitFor Delay '00:00:45';SELECT name FROM sys.tables;"

    Dim mySqlConnectionFormat As String = "data source={0};initial catalog={1};user id={2};password={3};Trusted_Connection=False;Connect Timeout=600;Application Name=GhostInTheMachine;"

    Dim MyConn As New SqlConnection

    MyConn.ConnectionString = String.Format(mySqlConnectionFormat, ".", "SandBox", "Noobie", "NotARealPassword")

    MyConn.Open()

    'now lets get a command object

    Dim mySqlCommand As New SqlCommand

    mySqlCommand.Connection = MyConn

    mySqlCommand.CommandTimeout = 600

    mySqlCommand.CommandType = CommandType.Text

    mySqlCommand.CommandText = sqlcmd

    Dim myDataReader As SqlDataReader

    myDataReader = mySqlCommand.ExecuteReader

    ' could either loop thru the reader itself, or stick it into a datatable .

    Dim MyDataTable As New DataTable

    MyDataTable.Load(myDataReader)

    'do something witht eh data?

    For Each dr As DataRow In MyDataTable.Rows

    Debug.Print(dr!name) ' [name] because i Know the column name from the query.

    Next

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!