asp.net开发sql server转换成oracle的方法详解
编程学习 2021-07-04 22:41www.dzhlxh.cn编程入门
这篇文章主要给大家介绍了关于asp.net开发中sql server转换成oracle的相关资料,文中通过示例代码和图文将实现的步骤一步步介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化来一起学习学习吧。
前言
因为前段时间我们公司项目 要把sql server 转oracle,发现网上这方面的资料较少,所以在这里分享一下心得,也记录一下问题,狼蚁网站SEO优化话不多说了,来一起看看详细的介绍:
开始我研究了一段时间 然后下载了
oracle 11g 版本 和 PL/SQL(客户端) 和sql server 不同的是 oracle 没有自己的客户端 需要用第三方的软件运行 PL/SQL 就是一个 sqldeveloper 也是一个,PL/SQL 我觉得比较稳定一点。但是2个都安装的话 刚好互补了
oracle 容易出现 无监听 什么 的错误 可以参考
然后再用
创建表空间 和用户 这些网上都找得到的
好了 东西都安装好了 狼蚁网站SEO优化开始 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); } /