系列文章導(dǎo)航: 如何將數(shù)據(jù)導(dǎo)入到 SQL Server Compact Edition 數(shù)據(jù)庫(kù)中(一) 如何將數(shù)據(jù)導(dǎo)入到 SQL Server Compact Edition 數(shù)據(jù)庫(kù)中(二) 如何將數(shù)據(jù)導(dǎo)入到 SQL Server Compact Edition 數(shù)據(jù)庫(kù)中(三) 摘要:在本系列文章的第一篇和第二篇為了提高數(shù)據(jù)寫(xiě)
系列文章導(dǎo)航:摘要:在本系列文章的第一篇和第二篇為了提高數(shù)據(jù)寫(xiě)入的性能,我使用了 SqlCeResultSet 基于表的數(shù)據(jù)寫(xiě)入方式,而不是使用常規(guī)的 Insert 語(yǔ)句。使用 SqlCeResultSet 寫(xiě)入數(shù)據(jù)確實(shí)方便又快速,但是必須保證從源數(shù)據(jù)庫(kù)查詢的結(jié)果集(通過(guò) Select 查詢語(yǔ)句)跟目標(biāo)數(shù)據(jù)庫(kù)(SQL Server Compact Edition)表的字段先后順序一致。如果不一致,可能會(huì)導(dǎo)致數(shù)據(jù)導(dǎo)入出錯(cuò);即便是導(dǎo)入成功,數(shù)據(jù)跟原來(lái)的字段位置也對(duì)不上。所以,我覺(jué)得有必要給大家介紹常規(guī)的 Insert 語(yǔ)句數(shù)據(jù)插入方式,解決 SqlCeResultSet 存在的問(wèn)題。
在第三篇文章中,我們學(xué)習(xí)了 IDataReader.GetSchemaTable 方法,它可以返回一個(gè)描述了 DataReader 查詢結(jié)果中各列的元數(shù)據(jù)的 DataTable。在前面的文章介紹的數(shù)據(jù)導(dǎo)入方法中,都是使用 DataReader 從源數(shù)據(jù)庫(kù)讀取數(shù)據(jù)。那么從這個(gè) DataReader 獲取的 SchemaTable 信息,就可以用于生成插入數(shù)據(jù)的 Insert 語(yǔ)句,前提是源數(shù)據(jù)庫(kù)和目標(biāo)數(shù)據(jù)庫(kù)的表字段名稱一致,字段的先后順序可以不一樣。以下是根據(jù) SchemaTable 生成 Insert 語(yǔ)句的代碼:
// 通過(guò) DataReader 獲取 SchemaTable 信息
srcReader = srcCommand.ExecuteReader(CommandBehavior.KeyInfo);
DataTable scheamTable = srcReader.GetSchemaTable();
// 生成 SQL Server Compact Edition 數(shù)據(jù)插入 SQL 語(yǔ)句
StringBuilder sbFields = new StringBuilder();
StringBuilder sbParams = new StringBuilder();
string field, param;
DataRow schemaRow;
for (int i = 0; i < scheamTable.Rows.Count; i++)
{
if (i != 0)
{
sbFields.Append(", ");
sbParams.Append(", ");
}
schemaRow = scheamTable.Rows[i];
field = string.Format("[{0}]", schemaRow["ColumnName"]); //字段名稱
param = "@" + ((string)schemaRow["ColumnName"]).Replace(" ", "_"); //參數(shù)名稱
sbFields.Append(field);
sbParams.Append(param);
destCommand.Parameters.Add(param, null);
}
string insertSql = string.Format("INSERT INTO [{0}]({1}) VALUES({2})", destTableName, sbFields, sbParams);
destCommand.CommandText = insertSql;
生成 Insert 語(yǔ)句的代碼很簡(jiǎn)單,這里就不再詳細(xì)說(shuō)明了。準(zhǔn)備好了 Insert 語(yǔ)句,就可以開(kāi)始從源數(shù)據(jù)庫(kù)取數(shù)據(jù)并寫(xiě)入目標(biāo)數(shù)據(jù)庫(kù)了。這里我使用了 DataReader.GetValues 方法一次性讀取一整行數(shù)據(jù),再給 Insert 命令的參數(shù)賦值。代碼如下所示:
// 執(zhí)行數(shù)據(jù)導(dǎo)入
object[] values;
while (srcReader.Read())
{
values = new object[srcReader.FieldCount];
srcReader.GetValues(values);
for (int i = 0; i < values.Length; i++)
{
destCommand.Parameters[i].Value = values[i];
}
destCommand.ExecuteNonQuery();
}
本文的主要內(nèi)容到這里已經(jīng)介紹完了,我們可以結(jié)合第三篇文章介紹的根據(jù) SchemaTable 自動(dòng)生成創(chuàng)建表結(jié)構(gòu)的 SQL 語(yǔ)句的代碼,在導(dǎo)入數(shù)據(jù)前先自動(dòng)創(chuàng)建數(shù)據(jù)表結(jié)構(gòu)。相關(guān)的代碼如下:
// 通過(guò) DataReader 獲取 SchemaTable 信息
srcReader = srcCommand.ExecuteReader(CommandBehavior.KeyInfo);
DataTable scheamTable = srcReader.GetSchemaTable();
// 創(chuàng)建 SQL Server Compact Edition 表結(jié)構(gòu)
SqlCeCommand command = destConnection.CreateCommand();
command.CommandText = GenerateTableSchemaSql(scheamTable);
command.ExecuteNonQuery();
// 生成 SQL Server Compact Edition 數(shù)據(jù)插入 SQL 語(yǔ)句
StringBuilder sbFields = new StringBuilder();
StringBuilder sbParams = new StringBuilder();
......
通過(guò)遍歷 SQL Server 2000 的 Northwind 數(shù)據(jù)庫(kù)的每個(gè)用戶表,并將每個(gè)表的數(shù)據(jù)導(dǎo)入到一個(gè) SQL Server Compact Edition 數(shù)據(jù)文件 Northwind.sdf 中。代碼如下所示:
// 創(chuàng)建源 SQL Server 數(shù)據(jù)庫(kù)連接對(duì)象
string srcConnString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";
SqlConnection srcConnection = new SqlConnection(srcConnString);
// 創(chuàng)建目標(biāo) SQL Server Compact Edition 數(shù)據(jù)庫(kù)連接對(duì)象
string destConnString = @"Data Source=C:/Northwind.sdf";
SqlCeConnection destConnection = new SqlCeConnection(destConnString);
// 創(chuàng)建 SQL Server Compact Edition 數(shù)據(jù)文件
VerifyDatabaseExists(destConnString);
srcConnection.Open();
destConnection.Open();
// 復(fù)制數(shù)據(jù)
string[] tableNames = GetTableNames(srcConnection);
string query;
for (int i = 0; i < tableNames.Length; i++)
{
query = string.Format("SELECT * FROM [{0}]", tableNames[i]);
CopyTable(srcConnection, destConnection, query, tableNames[i]);
}
srcConnection.Close();
destConnection.Close();
同第二篇文章相比,本文中的 VerifyDatabaseExists 方法只創(chuàng)建 SQL Server Compact Edition 數(shù)據(jù)文件,不批量創(chuàng)建表結(jié)構(gòu),因?yàn)槲覀冇蒙狭?ldquo;自動(dòng)”的方法,不需要預(yù)先準(zhǔn)備好創(chuàng)建表結(jié)構(gòu)的 SQL 腳本。GetTableNames 和 GenerateTableSchemaSql 方法跟第三篇文章的一樣,這里不再解釋。
總結(jié):本文介紹了一種比 SqlCeResultSet 更安全的數(shù)據(jù)寫(xiě)入方式,并結(jié)合了第三篇文章中介紹的自動(dòng)生成創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)的 SQL 語(yǔ)句的方法,向大家展示了一種比較完善的 SQL Server Compact Edition 數(shù)據(jù)導(dǎo)入方法。在后續(xù)的文章中我會(huì)繼續(xù)深入下去,提供本方案在實(shí)際應(yīng)用中面臨的問(wèn)題的解決方法。
示例代碼下載:sqlce_data_import4.rar
作者:黎波
博客:http://upto.cnblogs.com/
日期:2008年2月9日
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com