2017-11-03 42 views
0

辞書からDBにデータを移動しようとすると、接続を確立できません。これは、関数のコードです:SQLサーバーとの接続を確立できません

   const string serverName = @"homer"; 
       const string database = "RealState_RiskEngine"; 
       const string databaseTable = "dbo.LDR_ACCOUNT_DATA"; 

       Dictionary<string, Dictionary<string, string>> aliasesAndDatesAndValues = dictionary; 
       string strCon = string.Format("Data Source={0}\\SQLSTANDARD;Initial Catalog={1};Integrated Security=SSPI", 
        serverName, database); 
       SqlConnection conn = new SqlConnection(strCon); 
       conn.Open(); 

       string SQLstr = string.Format("INSERT INTO {0} (ID_ACCOUNT, DATE_TRIMESTER, VALUE)" + 
        "VALUES ('@ID_ACCOUNT', '@DATE_TRIMESTER', '@VALUE')", databaseTable); 
       SqlCommand cmd = new SqlCommand(SQLstr, conn); 
       cmd.Parameters.Add("@ID_ACCOUNT", System.Data.SqlDbType.VarChar); 
       cmd.Parameters.Add("@DATE_TRIMESTER", System.Data.SqlDbType.Date); 
       cmd.Parameters.Add("@VALUE", System.Data.SqlDbType.Int); 


       foreach (var pair in aliasesAndDatesAndValues) 
       { 
        foreach (var internal_pair in pair.Value) 
        { 
         cmd.Parameters["@ID_ACCOUNT"].Value = pair.Key.ToString(); 
         DateTime dateAndTime = Convert.ToDateTime(internal_pair.Key.ToString()); 
         var date = dateAndTime.Date; 
         Console.WriteLine(date); 
         Console.ReadLine(); 

         cmd.Parameters["@DATE_TRIMESTER"].Value = date; 
         cmd.Parameters["@VALUE"].Value = int.Parse(internal_pair.Value); 
         cmd.ExecuteNonQuery(); 
        } 
       } 

私は、パラメータなしでSqlConnectionオブジェクトを使用している場合、私はこのスタックトレースを取得する:私はこれを取得よりも、私は、パラメータ(SqlConnection conn = new SqlConnection(strCon))とのSQLConnectionを持っている場合

System.InvalidOperationException: The ConnectionString property has not been initialized. 
    at System.Data.SqlClient.SqlConnection.PermissionDemand() 
    at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) 
    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) 
    at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) 
    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) 
    at System.Data.SqlClient.SqlConnection.Open() 
    at ConsoleApplication2.Program.fillUpLdrAccounData(Dictionary`2 dictionary) in c:\users\atsurkanu\documents\visual studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 354 

そして、 1つのスタックトレース:

at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) 
    at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) 
    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) 
    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) 
    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) 
    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) 
    at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) 
    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) 
    at System.Data.SqlClient.SqlConnection.Open() 
    at ConsoleApplication2.Program.fillUpLdrAccounData(Dictionary`2 dictionary) in c:\users\atsurkanu\documents\visual studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 354 
ClientConnectionId:00000000-0000-0000-0000-000000000000 
Error Number:-1,State:0,Class:20 
+0

あなたはからこれを呼び出しているところわからないが、名前付きインスタンスがサポートされているかどうかを確認https://github.com/dotnet/corefx/issues/ 4586 –

答えて

1

それはこの1で問題だった:

私の場合は

、この1のようにする必要があります:

SqlConnection conn = new SqlConnection("Server = name; Database = DBname; Integrated Security = True;"); 
関連する問題