Monday 22 September 2014

Read Connection settings from XML file C# and VB.Net

Using C#


public static string xxx = string.Empty;
    static string executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

    public static string Connection()
    {
        DataSet _ds = new DataSet("MyDataSet");
        _ds.ReadXml(executableLocation + "\\ServerInfo.xml");
        if (_ds.Tables[0].Rows.Count > 0)
        {
            xxx = "Data Source=" + _ds.Tables[0].Rows[0]["DataSource"] + ";Database=" + _ds.Tables[0].Rows[0]["DatabaseName"] + ";uid=" + _ds.Tables[0].Rows[0]["UserName"] + ";pwd=" + _ds.Tables[0].Rows[0]["Password"];
            return xxx;
        }
        return xxx;
    }



Using VB.Net


Public Shared xxx As String = String.Empty
Shared executableLocation As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

Public Shared Function Connection() As String
 Dim _ds As New DataSet("MyDataSet")
 _ds.ReadXml(executableLocation & "\ServerInfo.xml")
 If _ds.Tables(0).Rows.Count > 0 Then
  xxx = ((("Data Source=" + _ds.Tables(0).Rows(0)("DataSource") & ";Database=") + _ds.Tables(0).Rows(0)("DatabaseName") & ";uid=") + _ds.Tables(0).Rows(0)("UserName") & ";pwd=") + _ds.Tables(0).Rows(0)("Password")
  Return xxx
 End If
 Return xxx
End Function

No comments:

Post a Comment