Tuesday, April 6, 2010

Visual Basic Connection String and Sample Source Code

Connection string to connect to Sql Server database (Visual Basic .Net 2008)

'Declare in a Module area
Public mCon As String = "Data Source=SERVER\SQLEXPRESS;Initial Catalog=Payroll;Integrated Security=True;User ID=user"


'---Sample Code to  check record exist ----



Function Check_Record_Exist(ByVal pSQLstr As String) As Boolean
        Dim conSQL As New SqlClient.SqlConnection ' for database connection
        Dim AdaptSql As SqlClient.SqlDataAdapter ' adapter is use to update the dataset and datasource
        Dim Dats As New DataSet
        Check_Record_Exist = False
        conSQL.ConnectionString = mCon
        conSQL.Open()
        AdaptSql = New SqlClient.SqlDataAdapter(pSQLstr, conSQL)
        AdaptSql.Fill(Dats, "Inquiry")
        'If Dats.Tables.Count() > 1 Then
        If Dats.Tables.Count > 1 Then
            Check_Record_Exist = True
            MsgBox("Record already exist, please check your entry.", MsgBoxStyle.Information, "Found Record!")
        End If
    End Function





Connection string to connect to Sql Server database (Visual Basic 6)

Public Contst  Constr= "Provider=MSDASQL.1;Persist Security Info=False;Data Source=Test1;Mode=ReadWrite"

'--- Sample Code ----


Function Get_Item_Desc(pCode As String) As String
On Error GoTo errHandler
    Dim rst As New ADODB.Recordset
    Dim mSQL As String
    Get_Item_Desc = ""
    With rst
        mSQL = "Select * from ItemMast Where Itemcode = '" & pCode & "' "
        .Open mSQL, mCon, adOpenDynamic, adLockReadOnly
        If .BOF And .EOF Then
        Else
            Get_Item_Desc = Trim(Format(!Description))            
        End If
        .Close
    End With
    Exit Function
errHandler:
    MsgBox Err.Number & Err.Description
End Function





0 comments:

Post a Comment

 
Powered by Blogger