2016-11-21 16 views
0

C#コードを使用してSQL Serverに接続しようとしていますが、接続を確立できず、ユーザーabcdefの例外ログインに失敗しました。エラー:ユーザーSQL Serverのログインに失敗しました

SQL Server Management Studioを使用して同じ資格情報を使用してDBに接続すると、問題なく正常に動作します。

<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(); 
+0

接続文字列を呼び出すには、ユーザーが同様にDB "mytestDB" へのアクセス権を持っていることをしていますか? –

+0

はい、SSMS経由でそのDBから接続してクエリできます –

+0

ローカルマシンからssmsを実行していますか? –

答えて

0

.ToString()が接続文字列の正しい表現を返すかどうかはわかりません。とにかく、それは正しいものであろう:

string strDBCon = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 
+0

@PeterDeme、これは私の問題を解決するのに役立ちませんし、接続文字列が作成される方法を変更するつもりはありません。 –

+0

@mason申し訳ありませんが、今編集しました。 – PeterDeme

+0

@AjaySrikanth strDBConの値をデバッグできますか?それが正しいか? – PeterDeme

-1

たぶん、あなたが信頼できる接続を使用して、接続文字列に「真Trusted_Connection =」を追加する必要があります。

+0

'Trusted_Connection = True'は** Windows認証**のためであり、指定されたSql Severの資格情報は無視されます(http://stackoverflow.com/questions/1642483/when-using-trusted-connection-true- and-sql-server-authentication-will-this-effe) – stuartd

+0

はい、そうです。 Windows認証用です –

0
<connectionStrings> 
<add name="NameOFConnectionString" connectionString="Data Source=Server;Initial Catalog=DatabaseName;User ID=User;Password=Pwd" 
providerName="System.Data.SqlClient" /> 

その後、

SqlConnection con = new SqlConnection(); 
     con.ConnectionString = ConfigurationManager.ConnectionStrings["NameOFConnectionString"].ToString(); 
関連する問題