当我尝试将数据从Dictionary移动到DB时,我甚至无法建立连接。这是函数的代码:
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,我会得到这个堆栈跟踪:
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
如果我有参数(SqlConnection conn = new SqlConnection(strCon)
)的SQLConnection,那么我得到一个堆栈跟踪:
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 :(得分:1)
这是一个问题:
const permissions = [
{
permissionName: 'user_likes',
errorMessage: 'To give you better results we need your likes'
},
{
permissionName: 'user_birthday',
errorMessage: 'Would you like to receive a gift from us on your birthday?'
},
{
permissionName: 'user_friends',
errorMessage: 'We can\'t match you without knowing your friends'
}
];
// ....
login = ()=>{
LoginManager.setLoginBehavior('system_account')
LoginManager.logInWithReadPermissions(permissions.map((permission) => (permission.permissionName)))
.then(this.onLoginFinished)
.catch(this.onLoginError)
}
onLoginFinished = (result)=>{
if(result.isCancelled) {
// user canceled login manually
} else {
if (result.declinedPermissions && result.declinedPermissions.lenght > 0) {
// handle declined permissions
result.declinedPermissions.forEach((declinedPermission) => {
// match the declinedPermission with the permission object
}
} else {
// user gave all the permissions
}
AccessToken.getCurrentAccessToken().then((data)=>{
this.props.onLogin(data.accessToken.toString())
})
}
}
就我而言,应该是这样的:
SqlConnection conn = new SqlConnection(strCon);