I'm trying to connect to SQL Server using my C# code, but unable to establish the connection it keeps throwing the exception login failed for user abcdef.
When I use the same credentials to connect to DB through SQL Server Management Studio, it works fine without any issues.
<connectionStrings>
<add name="Conn"
connectionString="Server=xxxx\yyyy;User ID=abcdef;Password=******;Initial Catalog=mytestDB;"/>
</connectionStrings>
string strDBCon = ConfigurationManager.ConnectionStrings["Conn"].ToString();
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = strDBCon;
sqlConnection.Open();
I'm not sure whether .ToString()
gives back the correct representation of the connection string. Anyway, that would be the correct one:
string strDBCon = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
<connectionStrings>
<add name="NameOFConnectionString" connectionString="Data Source=Server;Initial Catalog=DatabaseName;User ID=User;Password=Pwd"
providerName="System.Data.SqlClient" />
then call the connection string
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["NameOFConnectionString"].ToString();
也许您使用可信连接,并且应该在连接字符串中添加“ Trusted_Connection = 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.