Reputation: 85
When I am trying to move data from Dictionary to DB, I can't even establish a connection. This is the code of the function:
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();
}
}
If I have SqlConnection without parameter, I get this stack trace:
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
And if I have SQLConnection with parameter (SqlConnection conn = new SqlConnection(strCon)
), than I get this one stack trace:
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
Upvotes: 0
Views: 1261
Reputation: 85
It was a problem with this one:
SqlConnection conn = new SqlConnection(strCon);
In my case, it should be like this one:
SqlConnection conn = new SqlConnection("Server = name; Database = DBname; Integrated Security = True;");
Upvotes: 1