這篇文章主要為大家介紹了關於asp.net開發中sql server轉換成oracle的相關資料,文中透過範例程式碼和圖文將實現的步驟一步步介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。
前言
因為前段時間我們公司專案要把sql server 轉oracle,發現網路上這方面的資料較少,所以在這裡分享一下心得,也記錄一下問題,下面話不多說了,來一起看看詳細的介紹:
開始我研究了一段時間然後下載了
oracle 11g 版本和PL /SQL(客戶端) 和sql server 不同的是oracle 沒有自己的客戶端需要用第三方的軟體運行PL/SQL 就是一個sqldeveloper 也是一個,PL/SQL 我覺得比較穩定一點。但2個都安裝的話剛好互補了
oracle 容易出現無監聽什麼的錯誤可以參考
http://www.jb51.net/article/91184.htm
接著再用
建立表格空間和使用者這些網路上都找得到的
好了東西都安裝好了下面開始sql server 轉oracle
首先是資料庫的轉換我試了很多種方式,都是多多少少都是有點問題,因為是2個不同的資料庫,最後我還是決定自己寫個程式轉換
程式碼貼出來
連結字串
<add key="OracleConnectionString" value="Password=123;User ID=SA;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost )(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)))"/> <add key="SqlServerConnectionString" value="server=localhost;database=Table;uid=sa;pwd=123"/>
##
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OracleClient; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TransplantSQL { public partial class Form1 : Form { public static string OracleConnectionString = System.Configuration.ConfigurationSettings.AppSettings["OracleConnectionString"]; public static string SqlServerConnectionString = System.Configuration.ConfigurationSettings.AppSettings["SqlServerConnectionString"]; public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { OracleConnection con = new OracleConnection(OracleConnectionString); try { con.Open(); if (con.State == System.Data.ConnectionState.Open) { label5.Text = "连接成功"; } } catch (OracleException se) { label5.Text = "连接失败"; } finally { con.Close(); } } private void button3_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(SqlServerConnectionString); try { con.Open(); if (con.State == System.Data.ConnectionState.Open) { label4.Text = "连接成功"; } } catch (SqlException se) { label4.Text = "连接失败"; } finally { con.Close(); } } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "") { DataTable tablenames = GetTableNames(); foreach (DataRow item in tablenames.Rows) { string tablename = item["Name"].ToString().ToUpper(); setdata(tablename); } } else { setdata(textBox1.Text); } label2.Text = "成功"; } private static void setdata(string tablename) { // 查找有没有此表 如果没有就加 int et = Convert.ToInt32(GetSingle("select count(*) from user_tables where table_name = '" + tablename + "'")); if (et <= 0) { DataTable tableInfo = GetTableInfo(tablename); string addtablesql = "CREATE TABLE {0}({1})"; string cs = string.Empty; string biaoshi = string.Empty; foreach (DataRow citem in tableInfo.Rows) { cs += citem["字段名"].ToString(); if (citem["类型"].ToString() == "int" || citem["类型"].ToString() == "bit" || citem["类型"].ToString() == "decimal") { cs += " NUMBER(" + (Convert.ToInt32(citem["长度"]) > 38 ? 38 : Convert.ToInt32(citem["长度"])) + (Convert.ToInt32(citem["小数位数"])>0?(","+Convert.ToInt32(citem["小数位数"])):"") + ")"; } else if (citem["类型"].ToString() == "nvarchar" || citem["类型"].ToString() == "float") { cs += " VARCHAR2(" + (Convert.ToInt32(citem["长度"]) == -1 ? 4000 : Convert.ToInt32(citem["长度"]) * 2) + ")"; } else if (citem["类型"].ToString() == "datetime") { cs += " DATE"; } cs += citem["主键"].ToString() == "1" ? " primary key " : ""; if (citem["标识"].ToString() == "1") { biaoshi = citem["字段名"].ToString(); } cs += citem["默认值"].ToString() != "" ? " default " + citem["默认值"].ToString() + " " : ""; cs += citem["允许空"].ToString() == "1" ? "," : " NOT NULL,"; } cs = cs.Substring(0, cs.Length - 1); string tempsql = string.Format(addtablesql, tablename, cs); GetSingle(tempsql); if (biaoshi != string.Empty) { #region 判断是否有序列号 没有就创建 就是自动标识 int xuliehao = 0; try { xuliehao = Convert.ToInt32(GetSingle(string.Format(@"select Seq_{0}.nextval from sys.dual", tablename))); } catch { } if (xuliehao <= 0) { #region 为了让序列不重复 取最大值为min 值 int max = Convert.ToInt32(GetSingle(string.Format("select max({1}) from {0}", tablename, biaoshi),null)); #endregion string sequence = string.Format(@"create sequence Seq_{0} start with {1} increment by 1 nomaxvalue minvalue 1 nocycle nocache", tablename, (max+1));//创建标识 GetSingle(sequence); } #endregion #region 创建序列的触发器 string chufaqisql = string.Format(@"CREATE OR REPLACE TRIGGER T_{0} BEFORE INSERT ON {0} FOR EACH ROW WHEN (new.{1} is null) begin select Seq_{0}.nextval into:new.{1} from dual; end;", tablename, biaoshi);//创建触发器 GetSingle(chufaqisql); #endregion #region 创建唯一约束 //string weiyisql = string.Format(@"create unique index U_{0} on {0} ({1})", tablename, biaoshi); //GetSingle(weiyisql); #endregion } //int count = Convert.ToInt32(GetSingle("SELECT count(1) FROM " + tablename)); //if (count < 10000) //{ DataSet ds = Query("SELECT * FROM " + tablename); DataTable dt = ds.Tables[0]; string columnsNames = string.Empty; string values = string.Empty; for (int i = 0; i < dt.Columns.Count; i++) { columnsNames += dt.Columns[i].ColumnName + ","; } columnsNames = columnsNames.Substring(0, columnsNames.Length - 1); foreach (DataRow dr in dt.Rows) { values = string.Empty; for (int i = 0; i < dt.Columns.Count; i++) { if (dr[i] != DBNull.Value) { if (dr[i].ToString() != "") { if (dt.Columns[i].DataType == Type.GetType("System.Double") || dt.Columns[i].DataType == Type.GetType("System.Decimal") || dt.Columns[i].DataType == Type.GetType("System.Int32")) { values += dr[i] + ","; } else if (dt.Columns[i].DataType == Type.GetType("System.String")) { values += "'" + dr[i].ToString().Replace('\'', '‘') + "',"; } else if (dt.Columns[i].DataType == Type.GetType("System.DateTime")) { values += "to_date('" + dr[i] + "','YYYY/MM/DD HH24:MI:SS'),"; } else if (dt.Columns[i].DataType == Type.GetType("System.Boolean")) { if (dr[i].ToString() == "False") { values += "0,"; } else { values += "1,"; } } } else { values += "chr(32),"; } } else { values += "NULL,"; } } values = values.Substring(0, values.Length - 1); string osql = "Insert into " + tablename + "(" + columnsNames + ") values(" + values + ")"; GetSingle(osql); } //} } } private static DataTable GetTableNames() { string sql = string.Format(@"SELECT Name FROM SysObjects Where XType='U' ORDER BY Name"); DataSet ds = Query(sql); return ds.Tables[0]; } private static DataTable GetTableInfo(string tableName) { string sql = string.Format(@"SELECT (case when a.colorder=1 then d.name else null end) 表名, a.colorder 字段序号,a.name 字段名, (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '1'else '' end) 标识, (case when (SELECT count(*) FROM sysobjects WHERE (name in (SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in (SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name))))))) AND (xtype = 'PK'))>0 then '1' else '' end) 主键,b.name 类型,a.length 占用字节数, COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度, isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,(case when a.isnullable=1 then '1'else '' end) 允许空, REPLACE(REPLACE(isnull(e.text,''),'(',''),')','') 默认值,isnull(g.[value], ' ') AS [说明] FROM syscolumns a left join systypes b on a.xtype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id left join sys.extended_properties f on d.id=f.class and f.minor_id=0 where b.name is not null And d.name='{0}' order by a.id,a.colorder", tableName); DataSet ds = Query(sql); return ds.Tables[0]; } public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(SqlServerConnectionString)) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds; } } public static object GetSingle(string SQLString) { using (OracleConnection connection = new OracleConnection(OracleConnectionString)) { using (OracleCommand cmd = new OracleCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, null); object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (OracleException e) { throw e; } } } } private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = CommandType.Text;//cmdType; if (cmdParms != null) { foreach (OracleParameter parameter in cmdParms) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } } public static object GetSingle(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(SqlServerConnectionString)) { using (SqlCommand cmd = new SqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { throw e; } } } } private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = CommandType.Text;//cmdType; if (cmdParms != null) { foreach (SqlParameter parameter in cmdParms) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } } } }這個程式碼適應我的資料庫轉換大家需要的話可以修改一下其中oracle 沒有自增長的,,而是序列另外序列可以用觸發器觸發麻煩了一點整的來說還算好啦然後就是把我原來的程式字串連結改成oracle 的連結上面貼出來了首先
System.Data.SqlClient; 引用改成
System.Data.OracleClient;
select * from (SELECT * FROM Table) where rownum<100其他格式轉換成字串格式 #select to_char(其他格式,字串格式) 如
select to_char(sysdate,'yyyy-mm-dd') from dual
字串截取
#(截取的字串,開始的位置,長度)select substr('111222',3,2) from dual字串格式轉換為時間格式
select to_date('2017-08-03','yyyy-mm-dd') from dualsql參數化佔位符,不能用@用:
SELECT * FROM Table where ID=:ID
系統時間
getdate()改成
sysdate
select Seq_Table.currval from dualSeq_Table是自動成長列的名字,每個表都不一樣,所以需要找到當前表設定的自動增長列對應的名字#都改完之後等程序不報錯了運行#會出現32和64位的什麼破問題網上各種說法的都有說項目屬性中改成X86 X64但是這些對我都不管用並且我覺得這些東西改了之後會對我原有的專案造成很多問題
所以我研究了很久最終得出幾個結果
首先如果是控制台或窗體程式的話直接把專案中首選32位勾上就行了Exp sa/123@ORCL file=C:\OracleBack\back_%date:~0,4%%date:~5,2%%date:~8,2%.dmp owner=sa IMP sa/123 BUFFER=64000 FILE=C:\OracleBack\back_20170821.dmp FROMUSER=SA TOUSER=SA
以上是asp.net教學之sql server轉換成oracle的方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

oracle asm指的是“自动存储管理”,是一种卷管理器,可自动管理磁盘组并提供有效的数据冗余功能;它是做为单独的Oracle实例实施和部署。asm的优势:1、配置简单、可最大化推动数据库合并的存储资源利用;2、支持BIGFILE文件等。

方法:1、利用“select*from user_indexes where table_name=表名”语句查询表中索引;2、利用“select*from all_indexes where table_name=表名”语句查询所有索引。

在Oracle中,可利用lsnrctl命令查询端口号,该命令是Oracle的监听命令;在启动、关闭或重启oracle监听器之前可使用该命令检查oracle监听器的状态,语法为“lsnrctl status”,结果PORT后的内容就是端口号。

在oracle中,可以利用“TO_SINGLE_BYTE(String)”将全角转换为半角;“TO_SINGLE_BYTE”函数可以将参数中所有多字节字符都替换为等价的单字节字符,只有当数据库字符集同时包含多字节和单字节字符的时候有效。

在oracle中,可以利用“drop sequence sequence名”来删除sequence;sequence是自动增加数字序列的意思,也就是序列号,序列号自动增加不能重置,因此需要利用drop sequence语句来删除序列。

在oracle中,可以利用“select ... From all_tab_columns where table_name=upper('表名') AND owner=upper('数据库登录用户名');”语句查询数据库表的数据类型。

方法:1、利用“LOWER(字段值)”将字段转为小写,或者利用“UPPER(字段值)”将字段转为大写;2、利用“REGEXP_LIKE(字符串,正则表达式,'i')”,当参数设置为“i”时,说明进行匹配不区分大小写。

方法:1、利用“alter system set sessions=修改后的数值 scope=spfile”语句修改session参数;2、修改参数之后利用“shutdown immediate – startup”语句重启服务器即可生效。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器