How to create connection string dynamically in C# [closed]

為{幸葍}努か 提交于 2019-12-10 10:08:29

问题


How to create connection string dynamically in C#, instead of creating it using string concatenation?


回答1:


You can create dynamic connection string with SqlConnectionStringBuilder Class in C# as follows.

for more details pls check http://msdn.microsoft.com/en-us/library/dce36088.aspx

private void Form1_Load(object sender, EventArgs e)
{
     SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder();
     connectionString.DataSource = @".\SQLEXPRESS";
     connectionString.InitialCatalog = "MyDatabase";
     connectionString.IntegratedSecurity = true;
     MessageBox.Show(connectionString.ConnectionString);
}



回答2:


you better use ADO .NET entity model. check this please: Tutoriel ADO .NET



来源:https://stackoverflow.com/questions/16511244/how-to-create-connection-string-dynamically-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!