Change Connection String After Creating Setup File in C#.NET

后端 未结 4 561
别那么骄傲
别那么骄傲 2020-12-18 10:14

I am creating a C# windows form applications, the working can be summarized as users fills some form and data is saved in Access database. Now the problem I am facing is tha

4条回答
  •  没有蜡笔的小新
    2020-12-18 10:41

    You can build ConnectionString at run-time by SqlConnectionStringBuilder

    // Create a new SqlConnectionStringBuilder and
        // initialize it with a few name/value pairs.
        SqlConnectionStringBuilder builder =
            new SqlConnectionStringBuilder(GetConnectionString());
    
        // The input connection string used the 
        // Server key, but the new connection string uses
        // the well-known Data Source key instead.
        Console.WriteLine(builder.ConnectionString);
    
        // Pass the SqlConnectionStringBuilder an existing 
        // connection string, and you can retrieve and
        // modify any of the elements.
        builder.ConnectionString = "server=(local);user id=ab;" +
            "password= a!Pass113;initial catalog=AdventureWorks";
    
        // Now that the connection string has been parsed,
        // you can work with individual items.
        Console.WriteLine(builder.Password);
        builder.Password = "new@1Password";
        builder.AsynchronousProcessing = true;
    
        // You can refer to connection keys using strings, 
        // as well. When you use this technique (the default
        // Item property in Visual Basic, or the indexer in C#),
        // you can specify any synonym for the connection string key
        // name.
        builder["Server"] = ".";
        builder["Connect Timeout"] = 1000;
        builder["Trusted_Connection"] = true;
        Console.WriteLine(builder.ConnectionString);
    
        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    

    Edit: you can use this : Finding SQL Servers on the Network

提交回复
热议问题