0

I actually have method for creating dynamically sql connection string that reads it's information from .txt file depending on path which is hard-coded. Now there is a problem that if the user installs exe of this project. The path should depend on where the user installed the exe program - usually Program files, so I need to search selected folder and search it for that .txt file. I was wondering about it for quiet long time. Searched through the internet but didn't find anything helpful. Atleast I will psot my current dynamic connection string.

May I please ask you for any kind of help? Thank you so much for your time. I appreciate that.

internal static class DataSource
    {
        private static string _ConnectionString;
        public static string ConnectionString
        {
            get
            {
                if (_ConnectionString == null)
                    _ConnectionString = FunctionToDynamicallyCreateConnectionstring();
                return _ConnectionString;
            }
        }
        private static string FunctionToDynamicallyCreateConnectionstring()
        {

            string path = "C:\\Users\\marek\\Documents\\Visual Studio 2012\\Projects\\tours\\tours\\sql_string.txt";
            StreamReader sr = new StreamReader(File.Open(path, FileMode.Open));

            SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();


            cb.DataSource = DecodeFrom64(sr.ReadLine());
            cb.InitialCatalog = DecodeFrom64(sr.ReadLine());
            cb.UserID = DecodeFrom64(sr.ReadLine());
            cb.Password = DecodeFrom64(sr.ReadLine());

            return cb.ToString();


        }
Marek
  • 3,555
  • 17
  • 74
  • 123
  • 1
    Check this link for getting the directory of your executable http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-net-in-a-console-app – Hna Aug 11 '13 at 20:06
  • Is there a specific reason why you cannot use a [.config](http://msdn.microsoft.com/en-us/library/ms229689%28v=vs.90%29.aspx) file? – Filburt Aug 11 '13 at 20:09
  • @Filburt May I ask for any guide ? – Marek Aug 14 '13 at 07:47

1 Answers1

0

If you want to give App.config a try you need the following steps:

  • Add a reference to the .NET framework System.Configuration assembly
  • Add an App.config file to your project: "Add->New Item...->Application Configuration File"

Edit the App.config file adding your database connection string:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="TheFriendlyNameOfMyConnection" connectionString="Provider=SQLNCLI10.1;Integrated Security=SSPI;Initial Catalog=YOUR_DATABASE_NAME;Data Source=YOUR_SQLSERVER_NAME;" />
  </connectionStrings>
</configuration>

(adjust the connectionstring to your specific needs)

Read and use the connectionstring inside your application:

namespace ConsoleApplication1
{
    using System.Configuration;

    public class Program
    {
        static void Main(string[] args)
        {
            var connectionstring = ConfigurationManager.ConnectionStrings["TheFriendlyNameOfMyConnection"].ConnectionString;
        }
    }
}

The App.config file will be copied to your build output with the same name as your project assembly and the extension .config.

Recommended further reading: Connections Strings and Configuration Files

Filburt
  • 17,626
  • 12
  • 64
  • 115