记一次SQL Server2005导入Oracle10G的折腾过程【供多种数据库导入导出数据的C#程序源码参考】...
曾經(jīng)很早的時候用?SQL Server2000 時就見過我們老大,用?SQL Server2000 的導(dǎo)入導(dǎo)出功能,把數(shù)據(jù)都導(dǎo)入到了?Oracle9 里去,所以我也對導(dǎo)入導(dǎo)出數(shù)據(jù)有充分的信心,絕對能做好。
??
?? 客戶需要一個Oracle版本的通用權(quán)限管理系統(tǒng)組件的數(shù)據(jù)庫,而且為此還支付了5000元的技術(shù)咨詢費(fèi)。最近一段時間Oracle用得不多,電腦里沒也裝Oracle,想來想去應(yīng)該家里的移動硬盤里,有Oracle11G,就裝了一個,裝好后總感覺用戶創(chuàng)建的不正確,折騰來折騰去,又卸載又重新裝了一下,這個前后估計(jì)耗費(fèi)了2-3個小時。
?? 但是想從?SQL Server2005 導(dǎo)入到Oracle11G時總是出現(xiàn)錯誤,折騰了1-2個小時也沒能搞定,接著又嘗試從?SQL Server2008 導(dǎo)入到?Oracle11G還是不行照樣有錯無法正常導(dǎo)入,這時候簡直有些絕望了,唉連微軟的工具都無法正常導(dǎo)入導(dǎo)出數(shù)據(jù)。
?? 這時候我就咨詢?nèi)豪锏呐笥?#xff0c;有一個做開發(fā)的朋友推薦用?Kettle?,這個數(shù)據(jù)庫導(dǎo)入導(dǎo)出工具好不容易在網(wǎng)上下載了一個,無法正常運(yùn)行,又下載了JDK什么的,接著還說缺這個文件缺哪個文件,然后朋友又發(fā)了那些缺的文件,但是程序還是無法正常運(yùn)行,折騰這些,足足消耗了2個小時以上,家里的網(wǎng)絡(luò)只有2M,下載的速度也不太快。
?? 所以用一些不熟悉的東西,有一定的風(fēng)險,同時可能還需要很多學(xué)習(xí)成本。我電腦用的是win7 32位的,始終沒明白為啥?Kettle?沒能正常運(yùn)行。
?
?? 有一個比較擅長開發(fā)Oralce的朋友又推薦我用sqldeveloper-3.0.03.45.zip,但是死活找不到相關(guān)下載,好不容易Oracle的下載里有一個,但是也無法下載,版本也過低,由于這個軟件也比較龐大,客戶也無法從網(wǎng)上很順利的發(fā)過來,左找右找,又耗費(fèi)了1-2個小時,順便看看相關(guān)的文章,折騰了半死,結(jié)果沒能突破,再次想用工具的想法被破滅了。
?
?? 有朋友推薦用微軟的SQL Server Business Intelligence Development Studio 來折騰導(dǎo)入導(dǎo)出,結(jié)果折騰1-2個小時,還是無法正確導(dǎo)入導(dǎo)出表結(jié)構(gòu)及數(shù)據(jù),又是失敗告終,真是折騰人啊。
?
?? 4次想用工具導(dǎo)入數(shù)據(jù)的嘗試,均告失敗后,真是有些絕望了,足足一天時間就被這么浪費(fèi)掉了。只能硬這頭皮自己上了,客戶的問題總需要解決。?
?? 實(shí)在是沒辦法那就自己動手導(dǎo)入吧,從?PowerDesigner 15 的模型里把數(shù)據(jù)庫表結(jié)構(gòu)都折騰好,導(dǎo)入到?Oracle11G 結(jié)果發(fā)現(xiàn)表名被區(qū)別大小寫了,又把所有創(chuàng)建好的表都刪除了,折騰了1個小時,又把 表重新建立一遍, 把SQL語句里的“符號全部替換掉了,才把表結(jié)構(gòu)建立好了。
?? 實(shí)在忍無可忍了,自己寫了程序?qū)懥?個小時不到,把導(dǎo)入導(dǎo)出的程序?qū)懥艘槐?#xff0c;一執(zhí)行,幾分鐘就搞定了,唉,早知道這個效果早就自己寫個程序就好了,前后也就2-3個小時就可以了。
?
?? 好不容易導(dǎo)入?Oracle11G 成功后,客戶無法正確導(dǎo)入我提供的數(shù)據(jù)文件,客戶用的是??Oracle10G, 我又在電腦里裝了一個?Oracle10G 的客戶端,又把數(shù)據(jù)用?Oracle10G?導(dǎo)出一次,客戶才正確導(dǎo)入了數(shù)據(jù),這么一來回折騰又耗費(fèi)了好幾個小時。
??
?? 這期間程序的多數(shù)據(jù)庫兼容性測試也耗費(fèi)了幾個小時調(diào)試,其實(shí)光把?SQL Server2005導(dǎo)入Oracle10G 的折騰過程,足足耗費(fèi)了2天時間,人都有點(diǎn)兒累了,雖然嘴巴說說是很簡單的事情,真的折騰起來真是不只是腦力活兒還更是體力活兒。
?
?
?? 程序代碼的參考如下:
???//--------------------------------------------------------------------
//?All?Rights?Reserved?,?Copyright?(C)?2011?,?Hairihan?TECH,?Ltd.?//--------------------------------------------------------------------
using?System.Data;
namespace?DotNet.Example
{
????using?DotNet.BaseManager;
????using?DotNet.DbUtilities;
????public?class?ImportExportData
????{
????????///?<summary>
????????///?導(dǎo)出數(shù)據(jù)庫到Oralce
????????///?</summary>
????????public?void?Export()
????????{
????????????//this.ExportTable("Items_Area");
????????????//this.ExportTable("Items_AuditStatus");
????????????//this.ExportTable("Items_Degree");
????????????//this.ExportTable("Items_Duty");
????????????//this.ExportTable("Items_Education");
????????????//this.ExportTable("Items_Express");
????????????//this.ExportTable("Items_Links");
????????????//this.ExportTable("Items_MembershipLevels");
????????????//this.ExportTable("Items_Nationality");
????????????//this.ExportTable("Items_NewsCategory");
????????????//this.ExportTable("Items_OnSale");
????????????//this.ExportTable("Items_OrganizeCategory");
????????????//this.ExportTable("Items_Party");
????????????//this.ExportTable("Items_Pattern");
????????????//this.ExportTable("Items_PayCategory");
????????????//this.ExportTable("Items_PostCategory");
????????????//this.ExportTable("Items_RoleCategory");
????????????//this.ExportTable("Items_SalaryItemCategory");
????????????//this.ExportTable("Items_SendCategory");
????????????//this.ExportTable("Items_Sex");
????????????//this.ExportTable("Items_Title");
????????????//this.ExportTable("Items_Units");
????????????//this.ExportTable("Items_UserAuditStates");
????????????//this.ExportTable("Items_Wed");
????????????//this.ExportTable("Items_WorkCategory");
????????????//this.ExportTable("Items_WorkFlowCategories");
????????????//this.ExportTable("Items_WorkingProperty");
????????????//this.ExportTable("Base_Businesscard");
????????????//this.ExportTable("Base_Comment");
????????????//this.ExportTable("Base_Contact");
????????????//this.ExportTable("Base_ContactDetails");
????????????//this.ExportTable("Base_Exception");
????????????//this.ExportTable("Base_File");
????????????//this.ExportTable("Base_Folder");
????????????//this.ExportTable("Base_Items");
????????????//this.ExportTable("Base_Log");
????????????//this.ExportTable("Base_Message");
????????????//this.ExportTable("Base_News");
????????????//this.ExportTable("Base_Organize");
????????????//this.ExportTable("Base_Module",?"?SELECT?*?FROM?BASE_MODULE?ORDER?BY?PARENTID,?ID?");
????????????//this.ExportTable("Base_PermissionItem");
????????????//this.ExportTable("Base_Role");
????????????//this.ExportTable("Base_Staff");
????????????//this.ExportTable("Base_Parameter");
????????????//this.ExportTable("Base_Project");
????????????//this.ExportTable("Base_Permission");
????????????//this.ExportTable("Base_PermissionScope");
????????????this.ExportTable("Base_Sequence");
????????????
????????????//this.ExportTable("Base_StaffOrganize");
????????????//this.ExportTable("Base_TableColumns");
????????????//this.ExportTable("Base_User");
????????????//this.ExportTable("Base_UserAddress");
????????????//this.ExportTable("Base_UserOrganize");
????????????//this.ExportTable("Base_UserRole");
????????????//this.ExportTable("Base_StaffOrganize");
????????????//this.ExportTable("Base_WorkFlowActivity");
????????????//this.ExportTable("Base_WorkFlowCurrent");
????????????//this.ExportTable("Base_WorkFlowHistory");
????????????//this.ExportTable("Base_WorkFlowProcess");
????????????
????????????System.Console.ReadLine();
????????}
????????public?void?ExportTable(string?tableName)
????????{
????????????ExportTable(tableName,?tableName);
????????}
????????///?<summary>
????????///?導(dǎo)出一個表
????????///?</summary>
????????///?<param?name="tableName">表名</param>
????????///?<param?name="table">里面的數(shù)據(jù)</param>
????????public?void?ExportTable(string?tableName,?string?table)
????????{
????????????//?這里是獲取數(shù)據(jù)
????????????SqlHelper?sqlHelper?=?new?SqlHelper("Data?Source=localhost;Initial?Catalog=UserCenterV32;Integrated?Security=SSPI;");
????????????sqlHelper.Open();
????????????DataTable?dataTable?=?new?DataTable(tableName);
????????????if?(tableName.Equals(table))
????????????{
????????????????dataTable?=?sqlHelper.Fill("SELECT?*?FROM?"?+?table);
????????????}
????????????else
????????????{
????????????????dataTable?=?sqlHelper.Fill(table);
????????????}
????????????sqlHelper.Close();
????????????//?這里是插入數(shù)據(jù)
????????????OracleHelper?oracleHelper?=?new?OracleHelper("Data?Source=ORACLE11;user=DBO_USERCENTERV32;password=DBO_USERCENTERV32;");
????????????oracleHelper.Open();
????????????oracleHelper.BeginTransaction();
????????????SQLBuilder?sqlBuilder?=?new?SQLBuilder(oracleHelper);
????????????try
????????????{
????????????????//?清除表數(shù)據(jù)
????????????????oracleHelper.ExecuteNonQuery("TRUNCATE?TABLE?"?+?tableName);
????????????????//?創(chuàng)建配套的序列
????????????????//?oracleHelper.ExecuteNonQuery("create?sequence?SEQ_"?+?tableName?+?"?minvalue?1?maxvalue?999999999999999999999999?start?with?1?increment?by?1?cache?20");
????????????????int?r?=?0;
????????????????for?(r?=?0;?r?<?dataTable.Rows.Count;?r++)
????????????????{
????????????????????sqlBuilder.BeginInsert(tableName);
????????????????????for?(int?i?=?0;?i?<?dataTable.Columns.Count;?i++)
????????????????????{
????????????????????????sqlBuilder.SetValue(dataTable.Columns[i].ColumnName,?dataTable.Rows[r][dataTable.Columns[i].ColumnName]);
????????????????????}
????????????????????sqlBuilder.EndInsert();
????????????????????System.Console.WriteLine("表?"?+?tableName?+?"?已插入第?"?+?r.ToString()?+?"?行");
????????????????}
????????????????System.Console.WriteLine("?-?-?表?"?+?tableName?+?"?共插入?"?+?r.ToString()?+?"?行");
????????????????oracleHelper.CommitTransaction();
????????????}
????????????catch?(System.Exception?exception)
????????????{
????????????????oracleHelper.RollbackTransaction();
????????????????System.Console.WriteLine(tableName?+?"?--?"?+?exception.Message);
????????????}
????????????finally
????????????{
????????????????oracleHelper.Close();
????????????}
????????}
????}
}?
轉(zhuǎn)載于:https://www.cnblogs.com/Sue_/articles/2157234.html
總結(jié)
以上是生活随笔為你收集整理的记一次SQL Server2005导入Oracle10G的折腾过程【供多种数据库导入导出数据的C#程序源码参考】...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 艾伟:ASP.NET MVC,深入浅出I
- 下一篇: C# 中特性(Attribute)的使用