จิงๆ Database ส่วนใหญ่เนี่ย จะสามารถเปิด Connection ได้มากกว่า 1 Connection คับ นั่นจึงเป็นสาเหตุที่เราเรียก SQL Server/MySQL/Orable ว่า DBMS หรือ Database Management System อ่ะคับ มันจะมีความสามารถในการหลบหลีกให้เราเองว่า ข้อมูลที่ User เห็นในเวลาที่สั่ง Query จะเป็นอย่างไร รวมไปถึงจะเกิดอะไรขึ้น ถ้าเกิดมีคนกำลังสั่ง SELECT ขณะที่อีกคนสั่ง DELETE ซึ่ง SqlCe ก้อรองรับการติดต่อ มากกว่า 1 Connection พร้อมกันครับ
พอดีว่าวันนี้ผมว่าง (ซะที) คับ เลยลองทดสอบโค๊ดให้ ปรากฏว่า ด้วย SQL Compact Edition 3.5 ข้อจำกัดนี้ เหมือนจะหายไปนะครับ คือว่า ด้วย Connection คนละตัว ผมสามารถ Select หลัง Insert ได้ทันที โดยที่ไม่ต้อง Close ก่อน
string path = Assembly.GetExecutingAssembly().GetName().CodeBase;
string file = Path.Combine(Path.GetDirectoryName(path), "AppDatabase1.sdf");
SqlCeConnection conn = new SqlCeConnection(string.Format("Data Source={0}", file));
SqlCeConnection conn2 = new SqlCeConnection(string.Format("Data Source={0}", file));
SqlCeCommand cmd = conn.CreateCommand();
SqlCeCommand cmd2 = conn2.CreateCommand();
conn.Open();
conn2.Open();
cmd.CommandText = "INSERT INTO [table] VALUES('a')";
cmd.ExecuteNonQuery();
cmd2.CommandText = "SELECT * FROM [table]";
SqlCeDataReader reader = cmd2.ExecuteReader();
bool read = reader.Read(); // true
conn.Close();
reader = cmd2.ExecuteReader();
read = reader.Read(); //true