您好,登錄后才能下訂單哦!
asp.net中sql server如何轉(zhuǎn)換成oracle?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!
開始我研究了一段時(shí)間 然后下載了
oracle 11g 版本 和 PL/SQL(客戶端) 和sql server 不同的是 oracle 沒有自己的客戶端 需要用第三方的軟件運(yùn)行 PL/SQL 就是一個(gè) sqldeveloper 也是一個(gè),PL/SQL 我覺得比較穩(wěn)定一點(diǎn)。但是2個(gè)都安裝的話 剛好互補(bǔ)了。
然后再用
創(chuàng)建表空間 和用戶 這些網(wǎng)上都找得到的
好了 東西都安裝好了 下面開始 sql server 轉(zhuǎn) oracle
首先是數(shù)據(jù)庫的轉(zhuǎn)換 我試了很多種方式,都是多多少少都是有點(diǎn)問題,因?yàn)槭?個(gè)不同的數(shù)據(jù)庫,最后我還是決定自己寫個(gè)程序 轉(zhuǎn)換
代碼貼出來
鏈接字符串
<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["小數(shù)位數(shù)"])>0?(","+Convert.ToInt32(citem["小數(shù)位數(shù)"])):"") + ")"; } 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["標(biāo)識"].ToString() == "1") { biaoshi = citem["字段名"].ToString(); } cs += citem["默認(rèn)值"].ToString() != "" ? " default " + citem["默認(rèn)值"].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 判斷是否有序列號 沒有就創(chuàng)建 就是自動(dòng)標(biāo)識 int xuliehao = 0; try { xuliehao = Convert.ToInt32(GetSingle(string.Format(@"select Seq_{0}.nextval from sys.dual", tablename))); } catch { } if (xuliehao <= 0) { #region 為了讓序列不重復(fù) 取最大值為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));//創(chuàng)建標(biāo)識 GetSingle(sequence); } #endregion #region 創(chuàng)建序列的觸發(fā)器 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);//創(chuàng)建觸發(fā)器 GetSingle(chufaqisql); #endregion #region 創(chuàng)建唯一約束 //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) 標(biāo)識, (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 占用字節(jié)數(shù), COLUMNPROPERTY(a.id,a.name,'PRECISION') as 長度, isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小數(shù)位數(shù),(case when a.isnullable=1 then '1'else '' end) 允許空, REPLACE(REPLACE(isnull(e.text,''),'(',''),')','') 默認(rèn)值,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); } } } } }
這個(gè)代碼適應(yīng)于我的數(shù)據(jù)庫轉(zhuǎn)換 大家需要的話 可以修改一下
其中oracle 沒有自增長的 ,,而是序列 另外序列可以用觸發(fā)器觸發(fā) 麻煩了一點(diǎn) 整的來說 還算好啦
然后就是把我原來的程序 字符串鏈接改成oracle 的鏈接 上面貼出來了
首先System.Data.SqlClient;
引用 改成System.Data.OracleClient;
然后 在看報(bào)錯(cuò)的地方通通改掉就行了 從Sql 改為 Oracle 就行
然后我貼出一些 不報(bào)錯(cuò) 但是sqlserver 和oracle 不同的地方
查詢前多少條數(shù)據(jù)
select * from (SELECT * FROM Table) where rownum<100
其他格式轉(zhuǎn)化成字符串格式
select to_char(其他格式,字符串格式) 如select to_char(sysdate,'yyyy-mm-dd') from dual
字符串截取
(截取的字符串,開始的位置,長度)
select substr('111222',3,2) from dual
字符串格式轉(zhuǎn)化為時(shí)間格式
select to_date('2017-08-03','yyyy-mm-dd') from dual
sql參數(shù)化占位符,不能用@用:
SELECT * FROM Table where ID=:ID
系統(tǒng)時(shí)間
getdate()
改成sysdate
獲取當(dāng)前自動(dòng)增長列ID
select Seq_Table.currval from dual
Seq_Table是自動(dòng)增長列的名字,每個(gè)表都不一樣,所以需要找到當(dāng)前
表設(shè)置的自動(dòng)增長列對應(yīng)的名字
都改完之后 等程序不報(bào)錯(cuò)了 運(yùn)行
會出現(xiàn)32和64位的什么破問題 網(wǎng)上各種說法的都有 說項(xiàng)目屬性中 改成 X86 X64
但是這些對我都不管用 并且我覺得 這些東西改了之后 會對我原有的項(xiàng)目造成很多問題
所以我研究了很久 最終得出幾個(gè)結(jié)果
首先 如果是控制臺 或窗體 程序的話 直接把項(xiàng)目中 首選32位 勾上就行了
如果是 asp.net 頁面程序就沒這么簡單了
我本機(jī)是64位系統(tǒng) 然后我安裝oracle 和客戶端都是64位 按道理不應(yīng)該給我報(bào)什么64位錯(cuò)啊
然后網(wǎng)上說要安裝32位Instant Client Setup
然后我就下了一個(gè) 安裝了 程序就可以了 ,
但是我在服務(wù)器上面 再次用同樣的方法又不行了,找了很多資料 最終 重新安裝了一下iis好了(其實(shí)是要重新注冊一遍framework)
安裝32位的版本最好和oracle 版本一樣
那時(shí)候我下了很多個(gè)版本 調(diào)試 有的時(shí)候 安裝 Instant Client Setup的時(shí)候會卡在一個(gè)點(diǎn)不動(dòng),可以多試幾次 如果還是不行 在重新找一個(gè)吧!
安裝完后 在控制面板里面可以找到的
另外貼出備份 還原的 命令 用CMD打開 運(yùn)行 修改對應(yīng)參數(shù)
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
感謝各位的閱讀!看完上述內(nèi)容,你們對asp.net中sql server如何轉(zhuǎn)換成oracle大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。