整理幾段操作 SQL Server Mobile 數(shù)據(jù)庫(kù)的常用 C# 代碼,供剛剛接觸 SQL Server Mobile 開發(fā)的朋友參考。
1. 創(chuàng)建數(shù)據(jù)庫(kù)
2. 驗(yàn)證和修復(fù)數(shù)據(jù)庫(kù)
3. 壓縮數(shù)據(jù)庫(kù)
5. 合并復(fù)制
6. 遠(yuǎn)程數(shù)據(jù)訪問(RDA)
7. 使用 SqlCeResultSet
8. 處理 SqlCeException
參考:
SQL Server Mobile 2005 聯(lián)機(jī)叢書
MSDN Library
1. 創(chuàng)建數(shù)據(jù)庫(kù)
// 創(chuàng)建數(shù)據(jù)庫(kù)
File.Delete("Test.sdf");
SqlCeEngine engine = new SqlCeEngine(
"Data Source='Test.sdf';LCID=1033;Password=\"s$;2'!dS64\";Encrypt=TRUE;");
engine.CreateDatabase();
File.Delete("Test.sdf");
SqlCeEngine engine = new SqlCeEngine(
"Data Source='Test.sdf';LCID=1033;Password=\"s$;2'!dS64\";Encrypt=TRUE;");
engine.CreateDatabase();
2. 驗(yàn)證和修復(fù)數(shù)據(jù)庫(kù)
// 驗(yàn)證和修復(fù)數(shù)據(jù)庫(kù)
SqlCeEngine engine = new SqlCeEngine("Data Source=AdventureWorks.sdf");
if (false == engine.Verify())
{
MessageBox.Show("Database is corrupted.");
engine.Repair(null, RepairOption.RecoverCorruptedRows);
}
SqlCeEngine engine = new SqlCeEngine("Data Source=AdventureWorks.sdf");
if (false == engine.Verify())
{
MessageBox.Show("Database is corrupted.");
engine.Repair(null, RepairOption.RecoverCorruptedRows);
}
3. 壓縮數(shù)據(jù)庫(kù)
// 壓縮數(shù)據(jù)庫(kù)
// 通過從現(xiàn)有文件新建數(shù)據(jù)庫(kù)文件來(lái)回收 SQL Server Mobile 數(shù)據(jù)庫(kù)中浪費(fèi)的空間。
// 此方法還可用來(lái)更改數(shù)據(jù)庫(kù)的排序順序、加密或密碼設(shè)置。
// 該連接字符串指定一個(gè)指向?qū)⒂纱朔椒▌?chuàng)建的目標(biāo)數(shù)據(jù)庫(kù)的連接。
// 如果指定的數(shù)據(jù)庫(kù)已經(jīng)存在或者具有相同名稱的另一文件已經(jīng)存在,則會(huì)引發(fā)異常。
// 如果為連接字符串傳遞空字符串,則新的數(shù)據(jù)庫(kù)文件將改寫舊的數(shù)據(jù)庫(kù)文件,
// 但名稱保持不變。
SqlCeEngine engine = new SqlCeEngine("Data Source=AdventureWorks.sdf");
//engine.Compact(null);
engine.Compact("Data Source=; Password=a@3!7f$dQ;");
// 通過從現(xiàn)有文件新建數(shù)據(jù)庫(kù)文件來(lái)回收 SQL Server Mobile 數(shù)據(jù)庫(kù)中浪費(fèi)的空間。
// 此方法還可用來(lái)更改數(shù)據(jù)庫(kù)的排序順序、加密或密碼設(shè)置。
// 該連接字符串指定一個(gè)指向?qū)⒂纱朔椒▌?chuàng)建的目標(biāo)數(shù)據(jù)庫(kù)的連接。
// 如果指定的數(shù)據(jù)庫(kù)已經(jīng)存在或者具有相同名稱的另一文件已經(jīng)存在,則會(huì)引發(fā)異常。
// 如果為連接字符串傳遞空字符串,則新的數(shù)據(jù)庫(kù)文件將改寫舊的數(shù)據(jù)庫(kù)文件,
// 但名稱保持不變。
SqlCeEngine engine = new SqlCeEngine("Data Source=AdventureWorks.sdf");
//engine.Compact(null);
engine.Compact("Data Source=; Password=a@3!7f$dQ;");
4. 收縮數(shù)據(jù)庫(kù)
// 收縮數(shù)據(jù)庫(kù)
// 通過將空頁(yè)移動(dòng)到文件的結(jié)尾然后截?cái)嘣撐募?BR>// 來(lái)回收 SQL Server Mobile 數(shù)據(jù)庫(kù)中浪費(fèi)的空間。
// 與 Compact 方法不同,Shrink 方法不創(chuàng)建臨時(shí)數(shù)據(jù)庫(kù)文件,
// 而是將所有空頁(yè)和未分配的頁(yè)都移到了文件的結(jié)尾,然后截?cái),從而減小數(shù)據(jù)庫(kù)的總大小。
SqlCeEngine engine = new SqlCeEngine("Data Source=AdventureWorks.sdf");
engine.Shrink();
// 通過將空頁(yè)移動(dòng)到文件的結(jié)尾然后截?cái)嘣撐募?BR>// 來(lái)回收 SQL Server Mobile 數(shù)據(jù)庫(kù)中浪費(fèi)的空間。
// 與 Compact 方法不同,Shrink 方法不創(chuàng)建臨時(shí)數(shù)據(jù)庫(kù)文件,
// 而是將所有空頁(yè)和未分配的頁(yè)都移到了文件的結(jié)尾,然后截?cái),從而減小數(shù)據(jù)庫(kù)的總大小。
SqlCeEngine engine = new SqlCeEngine("Data Source=AdventureWorks.sdf");
engine.Shrink();
5. 合并復(fù)制
// 合并復(fù)制
// 實(shí)例化并配置 SqlCeReplication 對(duì)象
SqlCeReplication repl = new SqlCeReplication();
repl.InternetUrl = "http://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
repl.InternetLogin = "MyInternetLogin";
repl.InternetPassword = "<password>";
repl.Publisher = "MyPublisher";
repl.PublisherDatabase = "MyPublisherDatabase";
repl.PublisherLogin = "MyPublisherLogin";
repl.PublisherPassword = "<password>";
repl.Publication = "MyPublication";
repl.Subscriber = "MySubscriber";
repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";
// 創(chuàng)建一個(gè)本地 SQL Server Mobile 數(shù)據(jù)庫(kù)的訂閱
repl.AddSubscription(AddOption.CreateDatabase);
// 跟 SQL Server 數(shù)據(jù)庫(kù)進(jìn)行同步
repl.Synchronize();
// 清理 repl 對(duì)象
repl.Dispose();
// 實(shí)例化并配置 SqlCeReplication 對(duì)象
SqlCeReplication repl = new SqlCeReplication();
repl.InternetUrl = "http://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
repl.InternetLogin = "MyInternetLogin";
repl.InternetPassword = "<password>";
repl.Publisher = "MyPublisher";
repl.PublisherDatabase = "MyPublisherDatabase";
repl.PublisherLogin = "MyPublisherLogin";
repl.PublisherPassword = "<password>";
repl.Publication = "MyPublication";
repl.Subscriber = "MySubscriber";
repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";
// 創(chuàng)建一個(gè)本地 SQL Server Mobile 數(shù)據(jù)庫(kù)的訂閱
repl.AddSubscription(AddOption.CreateDatabase);
// 跟 SQL Server 數(shù)據(jù)庫(kù)進(jìn)行同步
repl.Synchronize();
// 清理 repl 對(duì)象
repl.Dispose();
6. 遠(yuǎn)程數(shù)據(jù)訪問(RDA)
// 遠(yuǎn)程數(shù)據(jù)訪問
// 實(shí)例化并配置 SqlCeRemoteDataAccess 對(duì)象
SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess();
rda.InternetUrl = "http://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
rda.InternetLogin = "MyInternetLogin";
rda.InternetPassword = "<password>";
rda.LocalConnectionString = "Data Source=MyDatabase.sdf";
// 從 SQL Server 下載數(shù)據(jù)
rda.Pull(
"Employees",
"SELECT * FROM DimEmployee",
"Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd=;",
RdaTrackOption.TrackingOnWithIndexes,
"ErrorTable");
//
// 修改本地?cái)?shù)據(jù)
//
// 將已修改的數(shù)據(jù)上傳到 SQL Server
rda.Push(
"DimEmployee",
"Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd=;");
// 提交 SQL 語(yǔ)句在 SQL Server 上執(zhí)行
rda.SubmitSql(
"CREATE TABLE MyRemoteTable (colA int)",
"Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd=;");
// 實(shí)例化并配置 SqlCeRemoteDataAccess 對(duì)象
SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess();
rda.InternetUrl = "http://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
rda.InternetLogin = "MyInternetLogin";
rda.InternetPassword = "<password>";
rda.LocalConnectionString = "Data Source=MyDatabase.sdf";
// 從 SQL Server 下載數(shù)據(jù)
rda.Pull(
"Employees",
"SELECT * FROM DimEmployee",
"Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd=;",
RdaTrackOption.TrackingOnWithIndexes,
"ErrorTable");
//
// 修改本地?cái)?shù)據(jù)
//
// 將已修改的數(shù)據(jù)上傳到 SQL Server
rda.Push(
"DimEmployee",
"Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd=;");
// 提交 SQL 語(yǔ)句在 SQL Server 上執(zhí)行
rda.SubmitSql(
"CREATE TABLE MyRemoteTable (colA int)",
"Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd=;");
7. 使用 SqlCeResultSet
// 使用 SqlCeResultSet
// 創(chuàng)建 SQL Server Mobile 數(shù)據(jù)庫(kù)連接
SqlCeConnection conn = new SqlCeConnection("Data Source=Northwind.sdf");
// 創(chuàng)建并配置 SqlCeCommand 對(duì)象
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM Orders";
// 創(chuàng)建 SqlCeResultSet 對(duì)象,并配置為可滾動(dòng)、可更新、檢測(cè)數(shù)據(jù)源更改
ResultSetOptions options = ResultSetOptions.Scrollable |
ResultSetOptions.Sensitive |
ResultSetOptions.Updatable;
SqlCeResultSet resultSet = cmd.ExecuteResultSet(options);
// 創(chuàng)建 ResultSetView 對(duì)象,配置為只顯示序號(hào)為 1,3,5,8 的列
ResultSetView resultSetView = resultSet.ResultSetView;
int[] ordinals = new int[] { 1,3,5,8};
resultSetView.Ordinals = ordinals;
// 將 ResultSetView 綁定到 DataGrid 控件
this.dataGrid.DataSource = resultSetView;
// 創(chuàng)建 SQL Server Mobile 數(shù)據(jù)庫(kù)連接
SqlCeConnection conn = new SqlCeConnection("Data Source=Northwind.sdf");
// 創(chuàng)建并配置 SqlCeCommand 對(duì)象
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM Orders";
// 創(chuàng)建 SqlCeResultSet 對(duì)象,并配置為可滾動(dòng)、可更新、檢測(cè)數(shù)據(jù)源更改
ResultSetOptions options = ResultSetOptions.Scrollable |
ResultSetOptions.Sensitive |
ResultSetOptions.Updatable;
SqlCeResultSet resultSet = cmd.ExecuteResultSet(options);
// 創(chuàng)建 ResultSetView 對(duì)象,配置為只顯示序號(hào)為 1,3,5,8 的列
ResultSetView resultSetView = resultSet.ResultSetView;
int[] ordinals = new int[] { 1,3,5,8};
resultSetView.Ordinals = ordinals;
// 將 ResultSetView 綁定到 DataGrid 控件
this.dataGrid.DataSource = resultSetView;
8. 處理 SqlCeException
// 處理 SqlCeException
public static void ShowErrors(SqlCeException e)
{
SqlCeErrorCollection errorCollection = e.Errors;
StringBuilder bld = new StringBuilder();
Exception inner = e.InnerException;
foreach (SqlCeError err in errs)
{
// 標(biāo)識(shí)錯(cuò)誤類型的 HRESULT 值,這些錯(cuò)誤不是 SQL Server CE 固有的
bld.Append("\r\nError Code: ").Append(err.HResult.ToString("X"));
// 對(duì)錯(cuò)誤進(jìn)行描述的文本
bld.Append("\r\nMessage: ").Append(err.Message);
// 獲取 SqlCeError 的本機(jī)錯(cuò)誤號(hào)
bld.Append("\r\nMinor Err.: ").Append(err.NativeError);
// 生成錯(cuò)誤的提供程序的名稱
bld.Append("\r\nSource: ").Append(err.Source);
// 遍歷前三個(gè)錯(cuò)誤參數(shù)。SQL Server CE 使用錯(cuò)誤參數(shù)來(lái)提供有關(guān)錯(cuò)誤的其他詳細(xì)信息。
foreach (int numPara in err.NumericErrorParameters)
{
// 雖然錯(cuò)誤可能存在參數(shù),但并非發(fā)生的所有錯(cuò)誤都返回參數(shù)。
// 如果發(fā)生某個(gè)錯(cuò)誤時(shí)沒有返回任何參數(shù),則該數(shù)組的值為 0。
if (numPara != 0)
{
bld.Append("\r\nNum. Par.: ").Append(numPara);
}
}
// 遍歷最后三個(gè)錯(cuò)誤參數(shù)。SQL Server CE 使用錯(cuò)誤參數(shù)來(lái)提供有關(guān)錯(cuò)誤的其他詳細(xì)信息。
foreach (string errPara in err.ErrorParameters)
{
// 雖然錯(cuò)誤可能存在參數(shù),但并非發(fā)生的所有錯(cuò)誤都返回參數(shù)。
// 如果發(fā)生某個(gè)錯(cuò)誤時(shí)沒有返回任何參數(shù),則該數(shù)組的值將為空字符串。
if (errPara != String.Empty)
{
bld.Append("\r\nErr. Par.: ").Append(errPara);
}
}
}
MessageBox.Show(bld.ToString());
}
public static void ShowErrors(SqlCeException e)
{
SqlCeErrorCollection errorCollection = e.Errors;
StringBuilder bld = new StringBuilder();
Exception inner = e.InnerException;
foreach (SqlCeError err in errs)
{
// 標(biāo)識(shí)錯(cuò)誤類型的 HRESULT 值,這些錯(cuò)誤不是 SQL Server CE 固有的
bld.Append("\r\nError Code: ").Append(err.HResult.ToString("X"));
// 對(duì)錯(cuò)誤進(jìn)行描述的文本
bld.Append("\r\nMessage: ").Append(err.Message);
// 獲取 SqlCeError 的本機(jī)錯(cuò)誤號(hào)
bld.Append("\r\nMinor Err.: ").Append(err.NativeError);
// 生成錯(cuò)誤的提供程序的名稱
bld.Append("\r\nSource: ").Append(err.Source);
// 遍歷前三個(gè)錯(cuò)誤參數(shù)。SQL Server CE 使用錯(cuò)誤參數(shù)來(lái)提供有關(guān)錯(cuò)誤的其他詳細(xì)信息。
foreach (int numPara in err.NumericErrorParameters)
{
// 雖然錯(cuò)誤可能存在參數(shù),但并非發(fā)生的所有錯(cuò)誤都返回參數(shù)。
// 如果發(fā)生某個(gè)錯(cuò)誤時(shí)沒有返回任何參數(shù),則該數(shù)組的值為 0。
if (numPara != 0)
{
bld.Append("\r\nNum. Par.: ").Append(numPara);
}
}
// 遍歷最后三個(gè)錯(cuò)誤參數(shù)。SQL Server CE 使用錯(cuò)誤參數(shù)來(lái)提供有關(guān)錯(cuò)誤的其他詳細(xì)信息。
foreach (string errPara in err.ErrorParameters)
{
// 雖然錯(cuò)誤可能存在參數(shù),但并非發(fā)生的所有錯(cuò)誤都返回參數(shù)。
// 如果發(fā)生某個(gè)錯(cuò)誤時(shí)沒有返回任何參數(shù),則該數(shù)組的值將為空字符串。
if (errPara != String.Empty)
{
bld.Append("\r\nErr. Par.: ").Append(errPara);
}
}
}
MessageBox.Show(bld.ToString());
}
參考:
SQL Server Mobile 2005 聯(lián)機(jī)叢書
MSDN Library