Results 1 to 9 of 9

Thread: Read config file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Read config file

    How to make this to be readed from the .ini file config.ini

    con.ConnectionString = "Data Source=localhost;Initial Catalog=MuOnline;Persist Security Info=True;User ID=sa;Password=passwordX"

    .ini
    [Settings]
    Host = localhost
    Username = sa
    Password = passwordX
    Database = MuOnline

    Code:
    Public Sub createTable(ByVal vdbs As String, ByVal file As String)
        username = frmlogin.txtusername.Text
        password = frmlogin.txtusername.Text
        vsvr = vServer
        vdb = Trim$(vdbs)
        strCon1 = "Server=" & vsvr & ";Database=" & vdb & ";uid=" & username & ";pwd=" & password & ";"
       sqlCon1 = New SqlClient.SqlConnection(strCon1)
        sqlCon1.Open()
        Dim arr() As String
        arr = Split(readTextFile(file), " GO ")
        Dim i As String
    
    For Each i In arr
            If i <> "" Then
                Dim cmd2 As New SqlClient.SqlCommand("" & i & "", sqlCon1)
                cmd2.CommandType = CommandType.Text
                cmd2.ExecuteNonQuery()
            End If
        Next
    End Sub
     Public Function readTextFile(ByVal file As String) As String
        Dim fso As New System.Object
        Dim ts As Scripting.TextStream
        Dim sLine As String
    
        fso = CreateObject("Scripting.FileSystemObject")
        ts = fso.openTextFile(file)
    
        Do Until ts.AtEndOfStream
            sLine = sLine & " " & ts.ReadLine
    
        Loop
        ts.Close()
        fso = Nothing
        readTextFile = sLine
    
    End Function
    Last edited by diablo21; Jun 19th, 2015 at 07:15 PM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Read config file

    I had created 1 Class.vb for ReadIni
    What next to do ?

    http://www.mentalis.org/soft/class.qpx?id=6
    Last edited by diablo21; Jun 19th, 2015 at 08:09 PM.

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Read config file

    Just Google "visual basic read text file" lots of examples.

    But why? You could you your project settings to store that information. Open up the project properties page and click on the Settings tab.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Read config file

    because i want to be custom configuration, not only me will use this simply program for edit database stuff
    some people too, so fast and easy way is to add settings from .ini file so they edit and Boom all okey they start to edit

    If i want to add my configs for my sql no point to ask here right?
    So i'm waiting someone atlast to help me out with this

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Read config file

    Are you saying that every user will be using a different database?

    If i want to add my configs for my sql no point to ask here right?
    There are lots of different ways to store that information. Remember an ini file is a text file.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Read config file

    i will just use then trusted connection will connect to sql server without asking username and password

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Read config file

    If your just wanting to store the username and password, then add a table to your database to store the information and then add a logon form to your project. Google "visual basic login form sql database"

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Read config file

    Quote Originally Posted by diablo21 View Post
    because i want to be custom configuration, not only me will use this simply program for edit database stuff
    some people too, so fast and easy way is to add settings from .ini file so they edit and Boom all okey they start to edit

    If i want to add my configs for my sql no point to ask here right?
    So i'm waiting someone atlast to help me out with this
    The built-in config file is also just a text file... it happens to be in XML format. You can set up the settings as User-level, which means each user would have their own settings... the reading & writing of it is baked right into the framework, making it easy-peasy to use. You also don't need to worry about folder security getting in the way - there's limits on where you can write info and Program Files is one of those. Since your app isn't writing, that's probably less of a problem.

    That said... the easiest way to read a text file - using IO.File.ReadAllText ... returns a string array containing the contents of the file. From there it's a simple matter of looping through it, seeing what each one StartsWith and capturing the right values.

    some thing like this - I'm shooting from the hip here, this is untested ...

    Code:
    Dim IniFile as String() = IO.File.ReadAllLines("Path to the INI file here")
    Dim dbLocation, dbUser, dbPwd, dbName as String
    
    for each s as string in IniFile
        If s.StartsWith("Host") Then
            dbLocation = s.Split("="c)(0).Trim
        ElseIf s.StartsWith("UserName") Then
            dbUser = s.Split("="c)(0).Trim
        ElseIf s.StartsWith("Password") Then
            dbPwd = s.Split("="c)(0).Trim
        ElseIf s.StartsWith("Database") Then
            dbName = s.Split("="c)(0).Trim
        end if
    
    Next
    
    'Use dbLocation, dbUser, dbPwd, dbName to assemble the connection string
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Read config file

    Quote Originally Posted by wes4dbt View Post
    If your just wanting to store the username and password, then add a table to your database to store the information and then add a logon form to your project. Google "visual basic login form sql database"
    Except that the OP is looking to store the username and password for the connection to the database... not the app... Actually, that might still work, just don't need the table... just ask for the login info, use it to build the connection string and Bob's you're uncle's cousin's former roommate.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width