none
How to connect to a database using a text file RRS feed

  • Question

  • Hello

    i created a class that serve what i want but it look to me not professionel so i want something more

    Imports System.IO
    Imports System.Data.SqlClient
    Public Class Class1
        Public Function MySub() As String
                Dim sr As New StreamReader("C:\Houssem\connection.ini")
                Dim line As String = sr.ReadLine
                Dim line2 As String = sr.ReadLine
                If line = "[SERVER]" Then
                    line2 = line2.Substring(4)
                End If
                Return (line2)
            End Function
            Public Function mysub2() As String
                Dim sr As New StreamReader("C:\Houssem\connection.ini")
                Dim line As String = sr.ReadLine
                Dim line1 As String = sr.ReadLine
                Dim line2 As String = sr.ReadLine
                If Not line2 = "" Then
                    line2 = line2.Substring(14)
                End If
                Return (line2)
            End Function
            Public Function cn() As SqlConnection
                Dim cnn As New SqlConnection("server='" & MySub() & "';database='" & mysub2() & "';integrated security=true;Connection Timeout=60")
                Return cnn
            End Function
    End Class

    i want place in text file some word to identify the server name for exemple

    [Database File]

    server:SDZ

    Database:SDZ

    so if i open the file and change SDZ i can connect to any database i want without recode the line of connection and its look not good for security

    please help


    • Edited by Houssem12 Wednesday, May 29, 2019 8:23 AM
    Wednesday, May 29, 2019 8:01 AM

Answers

All replies

  • Hi,

    I made a demo,you can try it:

    Imports System.Runtime.InteropServices
    Imports System.Text
    Imports System.Data.SqlClient
    Public Class Form1
        <DllImport("kernel32")>
        Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
        End Function
        Dim path As String = "C:\Houssem\connection.ini"
    
        Dim conn As SqlConnection
        Public Function IniReadValue(ByVal path As String, ByVal Section As String, ByVal Key As String) As String
            Dim temp As StringBuilder = New StringBuilder(500)
            Dim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 500, path)
            Return temp.ToString()
        End Function
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim constr As String = "server=" & IniReadValue(path, "Database File", "server") & ";database=" & IniReadValue(path, "Database File", "Database") & " ;uid=sa;pwd=xxx"
            Using conn = New SqlConnection(constr)
                conn.Open()
    
            End Using
        End Sub
    End Class
    

    ini file:

    [Database File]
    
    server=SDZ
    
    Database=SDZ

    Best  Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Wednesday, May 29, 2019 8:49 AM
    Moderator
  • thank you it works fine

    but i want to use it in every form in my project

    Wednesday, May 29, 2019 10:58 AM
  • I have an alternate method if interested which works in one project or multiple projects, the implementation is easy too.

    https://code.msdn.microsoft.com/Storing-SQL-Server-8b0dcefa?redir=0


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 29, 2019 11:15 AM
    Moderator
  • thank you for help and its hard to understand it

    i want put the file.ini with my project and after setup the application i can access and modify the line i want

    please help

    Wednesday, May 29, 2019 12:29 PM
  • thank you for help and its hard to understand it

    i want put the file.ini with my project and after setup the application i can access and modify the line i want

    please help

    https://blog.submain.com/app-config-basics-best-practices/
    Wednesday, May 29, 2019 12:35 PM
  • thank you for help and its hard to understand it

    i want put the file.ini with my project and after setup the application i can access and modify the line i want

    please help

    An INI file is not in use anymore. But you can make a config file. (Setup file) which can contain multiple servers 

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files


    Success
    Cor

    • Marked as answer by Houssem12 Thursday, May 30, 2019 1:20 PM
    Wednesday, May 29, 2019 2:57 PM