如何从文本文件中获取连接字符串?

Gop*_*opi 4 c# sql-server sql-server-2008 c#-4.0

我开发了一个带有SQL Server后端的Windows应用程序来插入员工姓名.我将逐个在三个数据库中插入员工详细信息.所以,我喜欢从文本文件中获取连接值.每当我想更改连接时,我只想在文本文件中输入登录详细信息.

如何从文本文件中获取连接字符串?

Moh*_*oho 5

使用app.config(MSDN)文件.

允许您配置多个命名连接字符串,您可以通过System.Configuration.ConfigurationManager类的ConnectionStrings属性访问它们


Reg*_*gon 4

请尝试这样

using System;
using System.IO;

class Test 
{

    public static void Main() 
    {
        string txtpath = @"c:\textfile.txt";
        try 
        {
            if (File.Exists(txtpath)) 
            {



            using (StreamReader sr = new StreamReader(txtpath)) 
            {
                while (sr.Peek() >= 0) 
                {
                    string ss = sr.ReadLine();
                     string [] txtsplit = ss.Split(';');

                     //now loop through   array
                     string server=txtsplit[0].Tostring();
                    string userid= split[1].Tostring(); // user id
                   string password= split[2].Tostring(); // password

                }
            }
          }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("Error: {0}", e.ToString());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)