I have the following code but it seems to not connect, just wondering whether i'm referring to the variable wrong?
sqlConnectionNW.ConnectionString = "Data Source=@server;Initial Catalog=Northwind;Integrated Security=True";
When I change it to:
sqlConnectionNW.ConnectionString = "Data Source=ISSP\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";
It works fine. It's just when I change it to the variable 'server'.
It would be more like this:
string mySQLServer = "ISSP\SQLEXPRESS";
sqlConnectionNW.ConnectionSTring = "Data Source=" + mySQLServer + ";Initial Catalog=Northwind;Integrated Security=True";
You can't have a variable like that in the connection string. You need to do any replacements before using the connection string.
你可以使用string.format(),这将允许你使用变量
sqlConnectionNW.ConnectionString = string.format("Data Source={0};Initial Catalog=Northwind;Integrated Security=True",server)
You maybe better off using the SqlConnectionStringBuilder class Sql Connection String Builder
This may allow you to generate the connection string in way more meaningful to you
当你在“”之间写一些东西时,它被识别为一个字符串,要使用一个变量,你必须在它之外写一个变量,并在它之前加一个+。如果有一些变量或字符串落后,你必须使用另一个+在您的变量之后。
"Data Source=" +variblename+ ";Initial Catalog=Northwind;Integrated Security=True"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.