A bug's life

导航

 
   因为工作需要,将原来在Oracle数据库中开发一个项目程序转移到Sql Server中。在数据库的导入导出的时候遇到了麻烦。使用SQL Server的dts不能导出Oracle中表的主键资料!这下麻烦了!!后来尝试使用PB的数据管道,可以是可以,不过就是需要逐个表的进行指定。表的数量不小,逐个指定比较浪费时间。所以就写了一个小程序,从Oracle中读出各个表的主键信息,然后生成符合T-SQL语法的生成主键语句
源程序如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DB;

namespace Ora2SQL
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.TextBox OraConn;
        
private System.Windows.Forms.TextBox SQLConn;
        
private System.Windows.Forms.DataGrid Grid;
        
private System.Windows.Forms.Button button2;
        
private System.Windows.Forms.RichTextBox SQLText;
        
private System.Windows.Forms.ComboBox DBType;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void button1_Click(object sender, System.EventArgs e)
        
{
            Database db 
= new Database();
            db.IsSqlDataBase 
= false;
            db.IsSql 
= true;
            db.ConnectionString 
= OraConn.Text;
            DataSet ds;
            
string SQL = "SELECT a.table_name,a.column_name FROM all_cons_columns a, all_constraints b WHERE a.owner = b.owner  AND a.constraint_name = b.constraint_name  AND a.table_name = b.table_name AND b.constraint_type = 'P' and a.owner = 'DICTDB'";
            db.RunProc(SQL,
out ds);
            Grid.DataSource 
= ds.Tables[0];
            
        }


        
private void button2_Click(object sender, System.EventArgs e)
        
{
            Database db 
= new Database();
            db.IsSqlDataBase 
= false;
            db.IsSql 
= true;
            db.ConnectionString 
= OraConn.Text;
            DataSet oratableds;
            DataSet orads;
            
string SQL = "SELECT distinct(a.table_name) as table_name  FROM all_cons_columns a, all_constraints b WHERE a.owner = b.owner   AND a.constraint_name = b.constraint_name   AND a.table_name = b.table_name   AND b.constraint_type = 'P'   and a.owner = '"+DBType.Text+"";
            
string ResultSQL = "";
            
bool flag;
            db.RunProc(SQL,
out oratableds);
            
string tablename="";
            
foreach(DataRow dr in oratableds.Tables[0].Rows){
                tablename
=Convert.ToString(dr["table_name"]);
                SQL
= "SELECT a.table_name,a.column_name FROM all_cons_columns a, all_constraints b WHERE a.owner = b.owner  AND a.constraint_name = b.constraint_name  AND a.table_name = b.table_name AND b.constraint_type = 'P' and a.owner = '"+DBType.Text+"' and a.table_name='"+tablename+"'";
                db.RunProc(SQL,
out orads);
                ResultSQL 
+= "ALTER TABLE "+tablename+" WITH NOCHECK ADD ";
                ResultSQL 
+= "    CONSTRAINT PK_"+tablename+" PRIMARY KEY  CLUSTERED ";
                ResultSQL 
+= "(";
                flag 
= false;
                
foreach(DataRow sdr in orads.Tables[0].Rows){
                    
if (flag)
                        ResultSQL 
+= ","+Convert.ToString(sdr["column_name"]);
                    
else
                        ResultSQL 
+= Convert.ToString(sdr["column_name"]);
                    flag
= true;
                }

                ResultSQL 
+= ")   go ";

            }
            
            SQLText.Text 
= ResultSQL;                    
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            
            DBType.Items.Add(
"DICTDB");
            DBType.Items.Add(
"USERDB");
        }

    }

}


目前的功能很简单,回头可以在改进改进。做成一个导入导出的小工具出来。
posted on 2004-08-13 08:22  Bugs  阅读(1950)  评论(0)    收藏  举报