how can i know if a database exist or not using vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veer
    New Member
    • Jul 2007
    • 198

    how can i know if a database exist or not using vb.net

    Hello

    i want to check wheather a database named "SvrName.md b" exist in "D:" or not if not then create and if yes then opendatabase

    i use the following code for creation and opening but i am not getting any idea how i can check
    Strapth="D:\"
    If Strpath = "SvrName.md b" Then

    dbConn = dbEngine.Create Database(Strpat h & "SvrName.md b", DAO.LanguageCon stants.dbLangGe neral)
    Else

    dbConn = dbEngine.OpenDa tabase(Strpath & "SvrName.md b", DAO.LanguageCon stants.dbLangGe neral)
    End If
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    This works in vb2005:

    [code=vb]
    Function FileExists(ByVa l FilePath)
    If System.IO.File. Exists(FilePath ) = True Then
    Return True
    Else
    Return False
    End If
    End Function
    [/code]

    There are many options with the 'system.io.'

    check them out

    James

    Comment

    • veer
      New Member
      • Jul 2007
      • 198

      #3
      thanks very much it works
      one another question
      i want to create table in that newely created database
      i did this in vb6.0 by declaring Tabledef
      and for each loop
      how can ido this in vb.net




      Originally posted by jamesd0142
      This works in vb2005:

      [code=vb]
      Function FileExists(ByVa l FilePath)
      If System.IO.File. Exists(FilePath ) = True Then
      Return True
      Else
      Return False
      End If
      End Function
      [/code]

      There are many options with the 'system.io.'

      check them out

      James

      Comment

      • jamesd0142
        Contributor
        • Sep 2007
        • 471

        #4
        This should work, i created it in vb2005.

        [code=vb]
        Private Sub DBConn(ByVal strSource As String, ByVal strQuery As String, ByVal dataset As System.Data.Dat aSet, ByVal DGV As DataGridView, ByVal tablename As String)
        Try
        '---- Imports System.Data.Ole Db - At top! ----
        '---Usage---
        'Dim oDS As New System.Data.Dat aSet
        'DBConn("c:\use rs\users.mdb", "select * from TBLUsers", oDS, DGV1, "table2")
        '---------
        Dim StrConn As String = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & strSource
        Dim oConn As New System.Data.Ole Db.OleDbConnect ion(StrConn)
        Dim da As System.Data.Ole Db.OleDbDataAda pter
        da = New OleDbDataAdapte r(strQuery, oConn)
        da.Fill(dataset , tablename)
        'can change DGV - to the name of your DataGrid
        DGV.DataSource = dataset.Tables( tablename).Defa ultView
        DGV.Refresh()
        da = Nothing
        dataset = Nothing
        oConn = Nothing
        Catch
        MsgBox("Did not execute correctly")
        End Try
        End Sub
        [/code]

        be sure to import ---- Imports System.Data.Ole Db - At top! ----

        You'll need to edit it to run an 'nonquery'

        something like:

        [code=vb]
        Try
        Dim StrConn As String = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=c:\Users \Users.mdb"
        Dim oConn As New System.Data.Ole Db.OleDbConnect ion(StrConn)
        Dim con As New System.Data.Ole Db.OleDbCommand
        con.Connection = oConn
        con.CommandType = CommandType.Tex t
        con.CommandText = "insert into tblusers (FirstName,Last Name,Login,[Password], Passwordchanged ) values (" & "'" & txtFirstName.Te xt & "'," & "'" & txtLastName.Tex t & "'," & "'" & txtLogin.Text & "'," & "'" & txtPassword.Tex t & "'," & "'" & strdate & "')"
        'add("insert into tblusers (FirstName,Last Name,Login,[Password], Passwordchanged ) values (" & "'" & "c" & "'," & "'" & "c" & "'," & "'" & "c" & "'," & "'" & "c" & "'," & "'" & strdate & "')")
        '"Update TBLUsers set [login] = " & "'" & "NEW" & "'" & " where [FirstName] = " & "'" & "ab" & "'"
        oConn.Open()
        con.ExecuteNonQ uery()
        oConn.Close()
        Catch
        End Try
        [/code]

        James

        Comment

        Working...