Hi peeps, pls i need help on how to hard code my database connect to my application . Example, i want to connect to my application using

dim conn as new SqlConnection
conn.ConnectionString = " connSring"

dim cmd as new SqlCommand
cmd.connection = conn
cmd.commandType = commandType.text
cmd.commandtext = " SELECT * FROM cUSTOMERS"

conn.Open ()

Dim Datread as SqlDataReader

Datread = cmd.ExecuteReader(CommandBehaviour.Default)

While DatRead.Read ()

DatRead("FirstName")

EndWhile

DatRead.Close
Conn.Close

Recommended Answers

All 3 Replies

You call that hard core? hehe ;p

Anyway, why when i read this word (hardcore) i imagine hard core porno ;p

you can create a module and then call the module whenever u need a database connection....

this will help you to update the changes in your database only once if u need to change it anytime in future,,,,else u need to change in each and every line if u dont create a module....

Module

Imports System.Data.SqlClient
Module Module1
       Public Connection As SqlConnection = New SqlConnection()
    Public Function Open_DB_Connection() As String
        Try
            Connection.ConnectionString = "Data Source = LocalHost\;Initial Catalog = Databasename;Integrated Security  = True"
            Connection.Open()
            Return "Success"
        Catch ex As Exception
            Return "Fail"
            Exit Function
        End Try
    End Function

    Public Function Close_DB_Connection() As String

        Try
            Connection.Close()
            Return "Success"
        Catch ex As Exception
             Return "Fail"
            Exit Function
        End Try

    End Function
End Module

when u need to call the database in your code...then call as

    'Open connection
         Dim Open_DB_Con As String
        Dim Close_DB_Con As String
        Open_DB_Con = Open_DB_Connection()
        If Open_DB_Con = "Success" Then
            Debug.Print("Database connection sucessfull")
        ElseIf Open_DB_Con = "Fail" Then
            MsgBox("Could not establish DataBase connection! ")
        End If

    Try
        Dim myCommand As SqlCommand
        myCommand = New SqlCommand("select * from Test", Connection)
        Dim DBReader As SqlDataReader = myCommand.ExecuteReader
        While DBReader.Read
           Label1.Text = DBReader("colname")
        End While
        DBReader.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

'closing connection
    Close_DB_Con = Close_DB_Connection()
    If Close_DB_Con = "Success" Then
        Debug.Print("Database connection closed")
    ElseIf Close_DB_Con = "Fail" Then
        MsgBox("Could not close DataBase connection! ")
        Debug.Print("Could not close DataBase connection! ")
    End If

Hope this helps....

Thanks man will work on that. but i need to ask a question.
is it non professional to use Dataset and configuration wizard ? cos i know much about that dataset, just want to know how to hard code.
Thanks Poojavb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.