說明:一般連線字串大多透過ConfigurationManager class
來讀取寫在web.config的連線字串,若沒web.config或想修改連線字串時,該如何做?
(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;)
作法:
使用SqlConnectionStringBuilder class:
public static SqlConnectionStringBuilder connStr;
public DataAccess()
{
connStr = new SqlConnectionStringBuilder();
connStr.DataSource = "IP";
connStr.InitialCatalog = "DB name";
connStr.UserID = "UserID";
connStr.Password = "Password";
}
參考資料:
http://stackoverflow.com/questions/16511244/how-to-create-connection-string-dynamically-in-c-sharp
沒有留言:
張貼留言