本文整理匯總了C#中System.Test.SubmitChanges方法的典型用法代碼示例。如果您正苦於以下問題:C# Test.SubmitChanges方法的具體用法?C# Test.SubmitChanges怎麽用?C# Test.SubmitChanges使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Test
的用法示例。
在下文中一共展示了Test.SubmitChanges方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: TestIdeneity
/// <summary>
/// 測試 Identity 的處理.
/// </summary>
private static void TestIdeneity()
{
Console.WriteLine("===== 測試父子關係表的 Identity 的處理.");
using (Test context = new Test(connString))
{
test_Identity_tab testMainData = new test_Identity_tab()
{
value = "測試主"
};
test_Identity_tab_Sub testSubData = new test_Identity_tab_Sub()
{
Value = "測試子"
};
testSubData.test_Identity_tab = testMainData;
// 插入.
context.test_Identity_tab.InsertOnSubmit(testMainData);
context.test_Identity_tab_Sub.InsertOnSubmit(testSubData);
Console.WriteLine("Before SubmitChanges testMainData.id = {0}", testMainData.id);
Console.WriteLine("Before SubmitChanges testSubData.id = {0}", testSubData.Id);
context.SubmitChanges();
Console.WriteLine("After SubmitChanges testMainData.id = {0}", testMainData.id);
Console.WriteLine("After SubmitChanges testSubData.id = {0}", testSubData.Id);
}
}
示例2: BaseTest
/// <summary>
/// Linq2SQL 基本功能測試
///
/// 基本的 查詢 / 插入 / 更新 / 刪除 處理.
///
/// </summary>
private static void BaseTest()
{
Test context = new Test(connString);
// 單表查詢
var query =
from testMain in context.TestMain
select testMain;
foreach (TestMain main in query)
{
Console.WriteLine("Main[{0}, {1}]", main.Id, main.Value);
}
// 關聯查詢
var query2 =
from testSub in context.TestSub
where testSub.TestMain.Value == "ONE"
select testSub;
foreach (TestSub sub in query2)
{
Console.WriteLine("Sub[{0}, {1}]", sub.Id, sub.Value);
}
// 插入.
TestMain main3 = new TestMain();
main3.Id = 3;
main3.Value = "Three";
context.TestMain.InsertOnSubmit(main3);
context.SubmitChanges();
Console.WriteLine("INSERT FINISH! --- Please Press Enter Ket");
Console.ReadLine();
// 更新.
var newTestMain =
(from testMain in context.TestMain
where testMain.Id == 3
select testMain).First();
newTestMain.Value = "Three3";
context.SubmitChanges();
Console.WriteLine("UPDATE FINISH! --- Please Press Enter Ket");
Console.ReadLine();
// 單表查詢 TOP 2
var queryTop2 =
(from testMain in context.TestMain
orderby testMain.Id descending
select testMain).Take(2);
foreach (TestMain main in queryTop2)
{
Console.WriteLine("Main[{0}, {1}]", main.Id, main.Value);
}
// 刪除.
context.TestMain.DeleteOnSubmit(newTestMain);
context.SubmitChanges();
Console.WriteLine("DELETE FINISH! --- Please Press Enter Ket");
Console.ReadLine();
}
示例3: TransactionTest
/// <summary>
/// 事務處理的測試.
///
/// 隱式創建事務
///
/// </summary>
private static void TransactionTest()
{
// 在我們沒有顯式的開啟事務時,
// DataContext.SubmitChanges在提交時會隱式創建事務
Test context = new Test(connString);
try
{
// 插入.
TestMain main3 = new TestMain();
main3.Id = 3;
main3.Value = "Three";
context.TestMain.InsertOnSubmit(main3);
TestMain main3x = new TestMain();
main3x.Id = 3;
main3x.Value = "Three Err";
context.TestMain.InsertOnSubmit(main3x);
context.SubmitChanges();
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
var query =
from testMain in context.TestMain
where testMain.Id == 3
select testMain;
Console.WriteLine("TestMain 表中, id=3 的記錄有{0}條 ", query.Count());
}