/// <summary> /// HowTO: 2. 基本数据库操作(基本表操作) /// </summary> static void HowTO_2() { Console.WriteLine("HowTO: 2. 直接使用AbstractTable进行表操作"); TestAbstractTable da = new TestAbstractTable(); //清空表 da.DeleteAll(); //插入表 int UserID = da.Insert("基本表操作", "基本表操作"); //查询结果 TestUser u = da.SelectUserByUserId(UserID); Console.WriteLine("Insert(values): ID={0},UserName={1}, UserPass={2}", u.ID, u.UserName, u.UserPass); u.UserName = "******"; u.UserPass = "******"; //Update<T>示例, 直接通过对象进行更新 da.Update <TestUser>(u); //查询DataSet DataSet ds = da.SelectDataSetByUserId(UserID); //Update<T>示例, 直接通过对象进行更新 if (ds.Tables[0].Rows.Count > 0) { da.Update <DataRow>(ds.Tables[0].Rows[0]); } //查询结果 u = da.SelectUserByUserId(UserID); Console.WriteLine("Update<T>结果: ID={0},UserName={1}, UserPass={2}", u.ID, u.UserName, u.UserPass); //Update(values) da.Update(u.ID, "参数化修改示例", "参数化修改示例"); u = da.SelectUserByUserId(UserID); Console.WriteLine("Update(values)结果: ID={0},UserName={1}, UserPass={2}", u.ID, u.UserName, u.UserPass); //Delete(主键) da.Delete(u.ID); u.UserName = "******"; u.UserPass = "******"; //Insert<T> UserID = da.Insert <TestUser>(u); //查询 u = da.SelectUserByUserId(UserID); Console.WriteLine("Inser<T>结果: ID={0},UserName={1}, UserPass={2}", u.ID, u.UserName, u.UserPass); }
/// <summary> /// HowTO: 5. Transaction, 支持两个不同数据库连接之间的事务 /// </summary> static void HowTO_5() { Console.WriteLine("HowTO: 5. Transaction, 支持两个不同数据库连接之间的事务"); TestAbstractDA da1 = new TestAbstractDA(); TestAbstractTable da2 = new TestAbstractTable(); try { using (TransScope scope = new TransScope(da1, da2)) { da1.DeleteAll(); da2.DeleteAll(); int id1 = da1.Insert("1", "1"); int id2 = da2.Insert("2", "2"); da1.Insert("3", "3"); da2.Insert("4", "4"); Console.WriteLine("da1:"); foreach (TestUser u in da1.SelectAll()) { Console.WriteLine("\tID={0},UserName={1},UserPass={2}", u.ID, u.UserName, u.UserPass); } Console.WriteLine("da2:"); foreach (TestUser u in da2.SelectAll()) { Console.WriteLine("\tID={0},UserName={1},UserPass={2}", u.ID, u.UserName, u.UserPass); } da1.Update(id1, "修改", "修改"); da2.Update(id2, "修改", "修改"); Console.WriteLine("da1修改后:"); foreach (TestUser u in da1.SelectAll()) { Console.WriteLine("\tID={0},UserName={1},UserPass={2}", u.ID, u.UserName, u.UserPass); } Console.WriteLine("da1修改后:"); foreach (TestUser u in da2.SelectAll()) { Console.WriteLine("\tID={0},UserName={1},UserPass={2}", u.ID, u.UserName, u.UserPass); } scope.Commit(); } } catch (DatabaseException ex) { Console.WriteLine(ex.Message); Console.WriteLine("SQL:" + ex.SQL); } }
/// <summary> /// HowTO: 2. �������ݿ����(���������) /// </summary> static void HowTO_2() { Console.WriteLine("HowTO: 2. ֱ��ʹ��AbstractTable���б����"); TestAbstractTable da = new TestAbstractTable(); //��ձ� da.DeleteAll(); //����� int UserID = da.Insert("���������", "���������"); //��ѯ��� TestUser u = da.SelectUserByUserId(UserID); Console.WriteLine("Insert(values): ID={0},UserName={1}, UserPass={2}", u.ID, u.UserName, u.UserPass); u.UserName = "******"; u.UserPass = "******"; //Update<T>ʾ��, ֱ��ͨ��������и��� da.Update<TestUser>(u); //��ѯDataSet DataSet ds = da.SelectDataSetByUserId(UserID); //Update<T>ʾ��, ֱ��ͨ��������и��� if (ds.Tables[0].Rows.Count > 0) { da.Update<DataRow>(ds.Tables[0].Rows[0]); } //��ѯ��� u = da.SelectUserByUserId(UserID); Console.WriteLine("Update<T>���: ID={0},UserName={1}, UserPass={2}", u.ID, u.UserName, u.UserPass); //Update(values) da.Update(u.ID, "��������ʾ��", "��������ʾ��"); u = da.SelectUserByUserId(UserID); Console.WriteLine("Update(values)���: ID={0},UserName={1}, UserPass={2}", u.ID, u.UserName, u.UserPass); //Delete(����) da.Delete(u.ID); u.UserName = "******"; u.UserPass = "******"; //Insert<T> UserID = da.Insert<TestUser>(u); //��ѯ u = da.SelectUserByUserId(UserID); Console.WriteLine("Inser<T>���: ID={0},UserName={1}, UserPass={2}", u.ID, u.UserName, u.UserPass); }
/// <summary> /// HowTO: 5. Transaction, ֧��������ͬ���ݿ�����֮������� /// </summary> static void HowTO_5() { Console.WriteLine("HowTO: 5. Transaction, ֧��������ͬ���ݿ�����֮�������"); TestAbstractDA da1 = new TestAbstractDA(); TestAbstractTable da2 = new TestAbstractTable(); try { using (TransScope scope = new TransScope(da1, da2)) { da1.DeleteAll(); da2.DeleteAll(); int id1 = da1.Insert("1", "1"); int id2 = da2.Insert("2", "2"); da1.Insert("3", "3"); da2.Insert("4", "4"); Console.WriteLine("da1:"); foreach (TestUser u in da1.SelectAll()) { Console.WriteLine("\tID={0},UserName={1},UserPass={2}", u.ID, u.UserName, u.UserPass); } Console.WriteLine("da2:"); foreach (TestUser u in da2.SelectAll()) { Console.WriteLine("\tID={0},UserName={1},UserPass={2}", u.ID, u.UserName, u.UserPass); } da1.Update(id1, "��", "��"); da2.Update(id2, "��", "��"); Console.WriteLine("da1�ĺ�:"); foreach (TestUser u in da1.SelectAll()) { Console.WriteLine("\tID={0},UserName={1},UserPass={2}", u.ID, u.UserName, u.UserPass); } Console.WriteLine("da1�ĺ�:"); foreach (TestUser u in da2.SelectAll()) { Console.WriteLine("\tID={0},UserName={1},UserPass={2}", u.ID, u.UserName, u.UserPass); } scope.Commit(); } } catch (DatabaseException ex) { Console.WriteLine(ex.Message); Console.WriteLine("SQL:" + ex.SQL); } }