none
VB.NET source coding needed RRS feed

  • Question

  • Please see the code given below and help me providing a code so that i can get "DESKTOP-8GF6DN4\SQLEXPRESS" from an ini (connection.ini) file. Its very important to me. Thanks for your help.

    connectionString="Data Source=DESKTOP-8GF6DN4\SQLEXPRESS;Initial Catalog=DeSchool_Mgt;Integrated Security=True"

    Monday, January 28, 2019 2:43 AM

All replies

  • Why an ini file, old-achool? Why can't you use a .net config file using the ConfiguratioManager() to get the connectionstring? 
    Monday, January 28, 2019 4:40 AM
  • Hi,

    know about GetPrivateProfileString function

    Imports System.Runtime.InteropServices
    Imports System.Text
    
    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 = "D:\test.ini"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = IniReadValue(path, "test", "PassWord")
        End Sub
        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
    End Class
    

    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.

    Monday, January 28, 2019 7:22 AM
  • Hello,

    First off, if always using SQL-Server Express then your connection string looks like this

    Data Source=.\SQLEXPRESS;Initial Catalog=DeSchool_Mgt;Integrated Security=True

    Where the .\ matches an instance of SQL-Server Express installed on a machine. So if you run the code on your machine or install the app using this connection string on another computer with Express installed it will simply work.

    Otherwise create a setting under project properties, settings tab. 

    To access the setting

    My.Settings.ConnectionString

    Simple code example to open a connection.

    Using cn = New SqlConnection With {.ConnectionString = My.Settings.ConnectionString}
        cn.Open()
    End Using
    Just stay away from .INI file, they are old and not used anymore except for very old apps or developer who don't know any better.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 28, 2019 10:45 AM
    Moderator
  • Just use the built-in .NET technology.

    https://www.aspsnippets.com/Articles/Read-Get-Connection-String-from-AppConfig-file-using-C-and-VBNet.aspx

    Monday, January 28, 2019 9:08 PM
  • Please see the code given below and help me providing a code so that i can get "DESKTOP-8GF6DN4\SQLEXPRESS" from an ini (connection.ini) file. Its very important to me. Thanks for your help.

    connectionString="Data Source=DESKTOP-8GF6DN4\SQLEXPRESS;Initial Catalog=DeSchool_Mgt;Integrated Security=True"

    • Merged by Alex Li-MSFT Tuesday, January 29, 2019 8:53 AM same case
    Tuesday, January 29, 2019 8:27 AM
  • Hi,

    I will merge these two same cases.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/60e29db5-3ae5-411d-afb1-fe4e67cfd4c2/vbnet-source-coding-needed?forum=vbgeneral

    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.

    Tuesday, January 29, 2019 8:51 AM