CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

救急呀!

楼主windflow(5biaomap.com.共享地图的快乐)2002-01-23 10:24:06 在 VB / 基础类 提问

有了对sql   server的足够权力(例如sa用户),如何在程序中删除sql   server中用户建的数据库?就象在enterprise   manager中删除一样。  
  我要对自己建的数据库进行更新。  
  问题点数:50、回复次数:3Top

1 楼dbcontrols(泰山__抛砖引玉)回复于 2002-01-23 10:27:35 得分 0

安装SQL客户端并用Distributed   Management   Objects   (SQL-DMO)对象Top

2 楼40Star(斯文、大方、有前途)回复于 2002-01-23 10:28:13 得分 10

DROP   DATABASE   (T-SQL)  
  Removes   one   or   more   databases   from   Microsoft®   SQL   Server™.   Removing   a   database   deletes   the   database   and   the   disk   files   used   by   the   database.  
   
  Syntax  
  DROP   DATABASE   database_name   [,...n]  
   
  Arguments  
  database_name    
  Specifies   the   name   of   the   database   to   be   removed.   Execute   sp_helpdb   from   the   master   database   to   see   a   list   of   databases.    
  Remarks  
  The   database   context   of   the   connection   must   be   in   the   master   database   to   use   DROP   DATABASE.  
   
  In   SQL   Server   version   7.0,   DROP   DATABASE   removes   damaged   databases   that   have   been   marked   suspect.   DROP   DATABASE   removes   the   specified   database   unless   it   has   been   marked   OFFLINE.  
   
  A   database   that   has   been   dropped   can   be   re-created   only   by   restoring   a   backup.   You   cannot   drop   a   database   currently   in   use   (open   for   reading   or   writing   by   any   user).   Whenever   a   database   is   dropped,   the   master   database   should   be   backed   up   .  
   
  Permissions  
  DROP   DATABASE   permission   defaults   to   the   database   owner   and   members   of   the   sysadmin   fixed   server   role,   and   is   not   transferable.  
   
  Examples  
  A.   Drop   a   single   database  
  This   example   removes   all   references   for   the   publishing   database   from   the   system   tables.  
   
  DROP   DATABASE   publishing  
   
       
   
  B.   Drop   multiple   databases  
  This   example   removes   all   references   for   each   of   the   listed   databases   from   the   system   tables.  
   
  DROP   DATABASE   pubs,   newpubs  
   
  Top

3 楼dbcontrols(泰山__抛砖引玉)回复于 2002-01-23 10:29:41 得分 40

Build   Your   Own   SQL   Enterprise   Manager   in   Visual   Basic   5.0  
  David   Mendlen  
  Director   of   Development,   Ameritech   Cellular  
  (mendlen@cellhe.ameritech.com)  
   
  October   1997  
   
  From   Regional   Director   Magazine  
   
  Click   to   copy   the   SQLOLE_EXE   sample   files.  
   
  Business   Problem   and   Solution  
  The   flexibility   and   functionality   of   the   Microsoft®   SQL   Server™   Enterprise   Manager   is   unmatched   in   the   enterprise   database   market.   This   functionality   is   not   limited   to   the   administrator   using   it.   The   power   of   the   Enterprise   Manager   is   available   to   the   Visual   Basic®   programmer   using   automation.   Thus   the   problem:   Design   a   reusable   ActiveX™   control   that   exposes   the   functionality   of   SQL   Server.  
   
   
   
  Figure   1.   New   functionality   to   create   and   delete   devices  
   
  The   solution   involves   combining   the   SQL   Distributed   Management   Objects   (DMO)   capabilities   of   Microsoft   SQL   Server   version   6.0   and   6.5   with   the   ActiveX   control   creation   functionality   of   Microsoft   Visual   Basic   version   5.0.   With   this   solution,   a   developer   can   drop   an   ActiveX   control   onto   a   Visual   Basic   form,   a   Visual   InterDev™   Web   page,   or   any   other   ActiveX   control–capable   environment.   The   newly   developed   control   has   the   ability   to   display   all   of   the   elements   that   are   visible   in   the   Enterprise   Manager.   Additionally,   I've   included   functionality   to   create   and   delete   devices   and   databases   (Figure   1).   The   solution   can   easily   be   extended   to   perform   a   variety   of   other   tasks   including   all   the   functionality   provided   in   the   enterprise   manager.   It's   very   easy   to   create   functionality   to   add,   delete,   and   change   tables,   views,   stored   procedures,   rules,   defaults,   and   user-defined   types.  
   
  SQL   Distributed   Management   Objects   Overview  
  Why   do   we   care   about   SQL   DMO?   The   programming   components   in   Microsoft   SQL   Server   6.0   let   you   develop   sophisticated   client/server   applications   that   tap   the   power   of   Microsoft   SQL   Server.   SQL   DMO   exposes   objects,   properties,   methods,   and   collections   that   are   used   to   write   scripts   and   programs   that   can   administer   multiple   SQL   Servers   across   a   distributed   network.   This   model   is   not   yet   another   data   access   alternative   like   ADO,   DAO,   and   so   on.   SQL   DMO   allows   you   to   create   database   administration   functions.   With   these   functions   you   could   publish   database   statistics   on   an   intranet.  
   
  In   order   to   access   the   SQL   DMO   objects   in   Visual   Basic   you   need   to   install   the   SQL   Server   client   objects   on   your   development   machine.   It   will   install   and   register   the   SQL   DMO   objects.   It   is   important   to   note   that   users   of   solutions   that   you   build   with   SQL   DMO   must   have   SQL   DMO   installed   through   the   same   process.  
   
  The   motivation   for   this   solution   was   a   dare.   A   Microsoft   BackOffice   Product   Manager   told   me   that   the   Enterprise   Manager   was   written   in   SQL   DMO.   When   I   argued   that   it   was   impossible   to   do,   the   Product   Manager   dared   me   to   build   my   own.   Before   we   get   into   the   ActiveX   control,   let's   take   a   closer   look   at   SQL   DMO.  
   
  Creating   a   SQLServer   Object  
  The   core   of   the   SQLDMO   object   model   is   the   SQLServer   object.   For   each   SQL   Server   you   want   to   connect   to,   you   create   an   empty   SQLServer   object,   then   you   connect   the   SQLServer   object   to   a   running   SQL   Server.  
   
  The   SQL   DMO   application   name   is   SQLOLE:  
   
  CreateObject("SQLOLE.SQLSERVER")  
   
  Early/Late   Binding  
  SQL   DMO   utilizes   vtable-binding   or   early   binding.   You   can   also   use   Dispatch   ID   binding   (dispID-binding)   which   uses   slower   performance   than   using   the   New   keyword   but   faster   than   the   generic   Object   type.  
   
  Using   the   generic   object   type  
  Dim   oSQLServer   As   Object  
  Set   oSQLServer   =   CreateObject   ("SQLOLE.SQLServer")  
   
  This   method   is   called   late   binding   using   OLE   IDispatch.  
   
  Connecting   to   SQL   Server  
  After   creating   a   new,   empty   SQLServer   object,   you   need   to   connect   that   object   to   a   running   SQL   Server   using   the   Connect   method   of   the   new   SQLServer   object.   You   pass   the   following   parameters   to   the   Connect   method:    
   
  SQL   Server   name  
   
   
  Login   ID  
   
   
  Password    
  Create   and   connect  
  Dim   SQLObject   As   SQLOLE.SQLServer  
   
  '   initialize   the   object   and   connect  
  Set   SQLObject=CreateObject   ("Sqlole.SQLServer")  
  SQLObject.Connect   "MyServer",   "SA",   "Password"  
   
  Databases   collection  
  You   can   iterate   through   the   list   of   databases   (from   the   SQL   Server   object)   with   the   databases   collection   using   a   For   .   .   Each   statement.  
   
  For   Each   database   in   SQLObject.Databases  
        Debug.Print   database.name  
  Next  
   
  Clean   up  
  '   Disconnect   SQLObject   from   SQL   Server  
  SQLObject.DisConnect  
  '   Good   programming   practice;   clean   up   objects   as   we   go  
  Set   SQLObject   =   Nothing  
   
  Figure   2   illustrates   the   SQL   DMO   object   model.  
   
   
   
  Figure   2.   The   SQL   DMO   object   Model  
   
  Solution   Architecture  
   
   
  Figure   3.   The   SQL   MDO   server-side   machine  
   
  To   use   this   solution,   you   need   two   computers.   One   is   the   server   running   Windows   NT®   and   SQL   Server.   The   other   runs   the   client   application   created   by   you.   Simply   drop   the   control   on   a   Visual   Basic   form   and   set   the   three   critical   properties:   SQL   Server,   login   ID,   and   password.   In   order   to   access   the   SQL   DMO   objects   on   your   development   machine,   you   must   have   installed   the   SQL   Server   client   components.   The   client   machine   could   be   running   Windows®   95   or   Windows   NT.  
   
   
   
  Figure   4.   The   SQL   DMO   client-side   machine  
   
  In   order   for   the   ActiveX   control   to   display   all   of   the   visual   elements   of   the   SQL   Enterprise   Manager   as   well   as   creating   and   deleting   devices,   we   only   need   to   talk   to   a   few   of   the   SQL   DMO   objects.   This   is   done   with   the   following   code:  
   
  Private   Sub   UserControl_ReadProperties(PropBag   As   PropertyBag)  
   
  The   first   step   is   to   connect   to   SQL   Server.   You   simply   provide   the   server   name,   a   login   ID,   and   a   password:  
   
  Set   objSQLServer   =   CreateObject("SQLOLE.SQLserver")  
     
    'Connect   to   SQL   Server  
    objSQLServer.Connect   ServerName:=pvtSQLServer,   login:=pvtLoginID,   Password:=pvtPassword  
    .  
    .  
    .  
   
  Next   we   connect   to   the   parent   object   of   the   connected   SQL   Server—the   application   object:  
   
  Dim   SQLApp   As   SQLOLE.Application  
    Set   SQLApp   =   objSQLServer.Application  
   
  Now   we   can   iterate   through   the   collection   of   SQL   Servers,   which   is   obtained   through   the   SQLServers   collection.   A   For   .   .   Each   statement   works   well   for   this   type   of   thing.  
   
  Dim   sServer   As   SQLOLE.SQLServer  
    For   Each   sServer   In   SQLApp.SQLServers  
   
  Add   the   SQL   Server   name   to   the   tree   view   control.   SServer.Name   is   the   name   of   the   SQL   Server:  
   
  Set   nodx   =   TreeView1.Nodes.Add("RTGroups",   tvwChild,   sServer.Name,   sServer.Name,   3)  
    .  
    .  
    .  
   
  Now   we   can   iterate   through   each   device   on   the   current   SQL   Server.   First   we   refresh   the   collection   of   devices   with   the   Refresh   method.  
   
  Dim   device   As   SQLOLE.device  
    sServer.Devices.Refresh  
   
  Again   we   can   iterate   through   the   collection   of   devices   on   the   current   SQL   Server   utilizing   a   For   .   .   Each   statement:  
   
  For   Each   device   In   sServer.Devices  
   
  Devices   come   in   two   flavors:   database   devices   and   dump   devices.   We   determine   the   database   type   and   add   the   device   to   the   treeview   control:  
   
  If   device.Type   =   0   Then   'Database   Devices  
    Set   nodx   =   TreeView1.Nodes.Add("RTDevices",   tvwChild,   "dv"   &   device.Name,   device.Name,   5)  
    Else  
    Set   nodx   =   TreeView1.Nodes.Add("RTDump   Devices",   tvwChild,   device.Name,   device.Name,   6)  
    End   If  
   
  You   access   the   database   collection   from   the   current   device.   Again   the   first   step   is   to   refresh   the   collection:  
   
  Dim   Database   As   SQLOLE.Database  
    sServer.Databases.Refresh  
   
  Accessing   the   databases   collection   is   done   a   little   differently—we   use   the   ListDatabases   collection.   I   added   a   function   that   takes   the   database   name   as   a   parameter   and   adds   the   database   and   its   child   objects   to   the   tree:  
   
  For   Each   Database   In   device.ListDatabases  
    AddDatabaseToTree   Database.Name  
    Next  
   
  Finally,   we   iterate   through   the   list   of   logins,   and   add   them   to   the   treeview:  
   
  Dim   login   As   SQLOLE.login  
    For   Each   login   In   sServer.Logins  
    Set   nodx   =   TreeView1.Nodes.Add("Logins",   tvwChild,   login.Name,   login.Name,   8)  
    Next  
    Next  
   
  Don't   forget   to   disconnect   from   SQL   Server:  
   
  objSQLServer.DisConnec  
   
  As   you   can   see,   communicating   with   the   DMO   objects   is   as   easy   as   manipulating   any   other   Visual   Basic   components.   This   example   can   easily   be   extended   to   surface   other   tasks.   For   example,   to   add   a   stored   procedure   to   SQL   Server   in   code   you   would   do   the   following:  
   
  Sub   AddStoredProcedureToDatabase(Database,   StoredProcedureName,   StoredProcedureText)  
   
  First   we   connect   to   the   appropriate   database   in   SQL   Server.   Then   we   connect   to   the   StoredProcedures   collection.   I   chose   to   do   it   all   in   one   line.  
   
  Dim   mySPs   As   SQLOLE.StoredProcedures  
    Set   mySPs   =   objSQLServer.Databases.Item(Database).StoredProcedures  
     
  Now   we   create   a   new   StoredProcedure   object   and   set   its   properties.   A   stored   procedure   has   two   critical   properties:   its   name   and   the   SQL   statement   that   you   want   executed.   The   SQL   statement   is   the   .TEXT   property.   You   notice   that   I've   filled   in   the   literal   string   "Create   Procedure"   and   appended   the   stored   procedure   name   as   well   as   the     literal   string   "as".   Combined   with   StoredProcedureText,   the   result   is   something   like   "Create   Procedure   Test   as   Select   *   from   authors."  
   
  Dim   mySP   As   SQLOLE.StoredProcedure  
    Set   mySP   =   CreateObject("SQLOLE.storedprocedure")  
    mySP.Name   =   StoredProcedureName  
    mySP.Text   =   "Create   Procedure   "   &   mySP.Name   &   "   as   "   &   StoredProcedureText  
   
  Now   all   we   need   to   do   is   add   the   newly   created   stored   procedure   to   the   stored   procedure   collection   we   connected   to   earlier:  
   
    mySPs.Add   mySP  
   
  Don't   forget   to   clean   up:  
   
    Set   mySP   =   Nothing  
    Set   mySPs   =   Nothing  
    objSQLServer.DisConnect  
   
  Using   the   ActiveX   Control  
  Step   1:   Create   a   new   Standard   .EXE   in   Visual   Basic   from   the   New   Project   window   as   shown   in   Figure   5.  
   
   
   
  Figure   5.   The   New   Project   window  
   
  Step   2:   Add   the   ActiveX   SQL   component   to   the   Toolbox.   Right-click   on   the   toolbox   and   select   Components…   Select   the   ActiveX   SQL   Library   from   the   list,   as   shown   in   Figure   6.   If   the   component   isn't   yet   registered,   click   the   Browse…   button   to   navigate   to   the   directory   of   the   ActiveXSQL.OCX   file.   Then   click   OK.  
   
   
   
  Figure   6.   The   Components   dialog   box  
   
  Step   3:   Draw   an   instance   of   the   ActiveXSQL   control   onto   your   form,   similar   to   the   one   shown   in   Figure   7  
   
   
   
  Figure   7.   Drawing   an   instance   of   the   ActiveXSQL   control  
   
  Step   4:   Right-click   on   the   control   and   select   Properties.   Fill   out   the   three   required   fields   and   click   OK,   as   shown   in   Figure   8.  
   
   
   
  Figure   8.   Filling   out   the   required   fields  
   
  Step   5:   Run   it!  
   
   
   
  Figure   9.   The   results  
   
  System   Requirements  
  Minimum   Requirements  
  PCs:   Intel®   486/33   or   higher  
   
   
  16   MB   of   RAM   for   Intel  
   
   
  Microsoft   Mouse   or   compatible   pointing   device  
   
   
  VGA   or   higher   resolution   monitor  
   
   
  High   density   (1.44   MB)   disk   drive    
  Recommended   Requirements  
  PCs:   Intel   Pentium   or   higher  
   
   
  16   MB   of   RAM   for   Intel  
   
   
  Microsoft   Mouse   or   compatible   pointing   device  
   
   
  VGA   or   higher   resolution   monitor  
   
   
  High   density   (1.44   MB)   disk   drive    
  Required   Software  
  Microsoft   Windows   95/Windows   NT  
   
   
  Visual   Basic   5.0   Enterprise   Edition  
   
   
  Microsoft   SQL   Server   6.0   or   6.5   (on   the   Server)    
  Summary  
  I   hope   you   enjoyed   this   example.   As   you   can   imagine,   the   possibilities   are   endless.   There   are   a   few   resources   that   you   should   turn   to   for   more   information   about   SQL   DMO.   Try   the   following   three   Web   sites.  
   
  http://www.bstone.com/SQLUG/SampleCode/samples.htm#DMO  
   
  http://www.windows.com/KB/Articles/q154/0/11.htm  
   
  http://www.ntmag.com/issues/jun96/sql.htm  
   
  Biography  
  David   Mendlen   is   the   director   of   development   for   Ameritech   Cellular.   David   is   a   Microsoft   Certified   Solution   Developer   and   a   certified   trainer   in   Visual   Basic   and   Windows   95.   He   is   also   the   founder   and   president   of   the   Chicago   Corporate   Visual   Basic   User   Group.   David   recently   worked   at   Microsoft   in   Redmond   on   solutions   that   will   ship   in   future   products   and   he   is   a   frequent   contributor   to   the   Cobb   Group   journals.  
  Top

相关问题

  • 救急!救急!
  • 救急,救急
  • 救急!救急!
  • 急救!急救!急救!急救!急救!急救!急救!急救!
  • 救急!!!!!!!
  • 救急!!!!!!
  • 救急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • 救急!!
  • 救急!!!
  • 救急!!!!!!!

关键词

  • database
  • microsoft
  • sql
  • server
  • activexsql
  • figure
  • dropped
  • higher
  • shown
  • removes

得分解答快速导航

  • 帖主:windflow
  • 40Star
  • dbcontrols

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo