Exemplo n.º 1
0
        public bool InsertTable(string DatabaseName, string TableName, DataTable dt)
        {
            SQLDMO.Application sqlApp  = new SQLDMO.ApplicationClass();
            SQLDMO.SQLServer   svr     = new SQLDMO.SQLServerClass();
            string             keyName = "";

            try
            {
                svr.Connect(ServerName, UserName, Password);
                SQLDMO.Database myDb = new SQLDMO.DatabaseClass();
                myDb = (SQLDMO.Database)svr.Databases.Item(DatabaseName, "owner");

                SQLDMO.Table myTb = new SQLDMO.TableClass();
                myTb.Name = TableName;
                foreach (DataRow dr in dt.Rows)
                {
                    SQLDMO.Column column = new SQLDMO.ColumnClass();
                    column.Name     = dr["ColumnName"].ToString();
                    column.Datatype = dr["ColumnType"].ToString();
                    column.Length   = int.Parse(dr["ColumnSize"].ToString());

                    if (bool.Parse(dr["ColumnKey"].ToString()))
                    {
                        keyName = column.Name;
                    }
                    column.AllowNulls = bool.Parse(dr["ColumnNull"].ToString());
                    myTb.Columns.Add(column);
                }

                if (keyName != "")
                {
                    SQLDMO.Key key = new SQLDMO.KeyClass();
                    key.Name = keyName;
                    key.Type = SQLDMO.SQLDMO_KEY_TYPE.SQLDMOKey_Primary;
                    key.KeyColumns.Add(keyName);
                    myTb.Keys.Add(key);
                }
                myDb.Tables.Add(myTb);

                return(true);
            }
            catch (Exception e)
            {
                //throw (new Exception("连接数据库出错:" + e.Message));
                ShowError("添加数据库失败!" + e.Message);
                return(false);
            }
            finally
            {
                svr.DisConnect();
                sqlApp.Quit();
            }
        }
Exemplo n.º 2
0
        public bool InsertTable(string DatabaseName, string TableName,DataTable dt)
        {
            SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
            SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass();
            string keyName = "";
            try
            {
                svr.Connect(ServerName, UserName, Password);
                SQLDMO.Database myDb = new SQLDMO.DatabaseClass();
                myDb = (SQLDMO.Database)svr.Databases.Item(DatabaseName, "owner");

                SQLDMO.Table myTb = new SQLDMO.TableClass();
                myTb.Name = TableName;
                foreach (DataRow dr in dt.Rows)
                {
                    SQLDMO.Column column = new SQLDMO.ColumnClass();
                    column.Name = dr["ColumnName"].ToString();
                    column.Datatype = dr["ColumnType"].ToString();
                    column.Length = int.Parse(dr["ColumnSize"].ToString());

                    if (bool.Parse(dr["ColumnKey"].ToString()))
                    {
                        keyName = column.Name;
                    }
                    column.AllowNulls = bool.Parse(dr["ColumnNull"].ToString());
                    myTb.Columns.Add(column);
                }

                if (keyName != "")
                {
                    SQLDMO.Key key = new SQLDMO.KeyClass();
                    key.Name = keyName;
                    key.Type = SQLDMO.SQLDMO_KEY_TYPE.SQLDMOKey_Primary;
                    key.KeyColumns.Add(keyName);
                    myTb.Keys.Add(key);
                }
                myDb.Tables.Add(myTb);

                return true;
            }
            catch (Exception e)
            {
                //throw (new Exception("�������ݿ�����" + e.Message));
                ShowError("������ݿ�ʧ��!" + e.Message);
                return false;
            }
            finally
            {
                svr.DisConnect();
                sqlApp.Quit();
            }
        }