我有一个SQL服务器,用于存储c#程序的各种设置。程序会定期检查这些设置。如果我要用查询手动更改设置,并且程序尝试同时读取它们,这会导致错误吗?换句话说,SQL“多任务”吗?只要没有任何事情崩溃,手动设置更改是否会延迟到程序的下一次检查无关紧要。
答案 0 :(得分:0)
几乎所有的RDBMS(包括Oracle,MS SQL,Interbase等)都是你所做的那样" multitask"。此外,RDMS专门设计用于这种模式(参见ACID for detailes)
https://en.wikipedia.org/wiki/ACID
唯一的诀窍是你不应该在不同的任务,线程等之间分享 Connection
:
Action action1 = () => {
using (SqlConnection conn = new SqlConnection(ConnectionStringHere)) {
conn.Open();
using (SqlCommand q = new SqlCommand()) {
q.Connection = conn;
q.CommandText = "update MyTable set x = 123 where y = 789";
q.ExecuteNonQuery();
}
}
};
Action action2 = () => {
using (SqlConnection conn = new SqlConnection(ConnectionStringHere)) {
conn.Open();
using (SqlCommand q = new SqlCommand()) {
q.Connection = conn;
q.CommandText = "delete from MyTable where y = 456";
q.ExecuteNonQuery();
}
}
};
// Let's call both actions in parallel (simultaneously)
Parallel.Invoke(action1, action2);