I have answered the question in the following forum. You can see it:)
http://expert.csdn.net/Expert/topic/1493/1493881.xml?temp=.1057855And if you think the method is OK. Pls give me the points:)
If the method has some problem, Pls contact me with the short message.

解决方案 »

  1.   

    dim rc as Integer
    dim str as string
    str = "isql -S servername -d databasename -U sa -P password -i createtable.sql -o createtable.out"
    rc = shell(str)这个方法好像只能在指定的数据库里建表,而不能新建一个数据库,我的想法是有一个textbox,点击“创建数据库”时,建立以textbox.text为名的数据库,然后再调用createtable.sql在其中建表,怎么解决呢
      

  2.   

    回复人: czjw(寒雪) ( ) 信誉:100  2003-03-05 20:56:00  得分:0 
     
     
      只要安装好sql2000,用osql建新库、建表、视图、索引等等,都是没有问题的。给你一个例子,你可以试一试:
    Dim rc As Integer
    Dim str As String
    str = "osql -i d:\wjw.sql -U sa -S 192.168.0.1 -P"
    rc = Shell(str)将以下代码用记事本以wjw.sql为名存在d:\ (执行后创建一个数据库wjw,一个用户表wujianwei)IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'wjw')
    DROP DATABASE [wjw]
    GOCREATE DATABASE [wjw]  ON (NAME = N'wjw_Data', FILENAME = N'D:\wjw_Data.MDF' , SIZE = 1, FILEGROWTH = 10%) LOG ON (NAME = N'wjw_Log', FILENAME = N'D:\wjw_Log.LDF' , SIZE = 1, FILEGROWTH = 10%)
     COLLATE Chinese_PRC_CI_AS
    GOexec sp_dboption N'wjw', N'autoclose', N'true'
    GOexec sp_dboption N'wjw', N'bulkcopy', N'false'
    GOexec sp_dboption N'wjw', N'trunc. log', N'true'
    GOexec sp_dboption N'wjw', N'torn page detection', N'true'
    GOexec sp_dboption N'wjw', N'read only', N'false'
    GOexec sp_dboption N'wjw', N'dbo use', N'false'
    GOexec sp_dboption N'wjw', N'single', N'false'
    GOexec sp_dboption N'wjw', N'autoshrink', N'true'
    GOexec sp_dboption N'wjw', N'ANSI null default', N'false'
    GOexec sp_dboption N'wjw', N'recursive triggers', N'false'
    GOexec sp_dboption N'wjw', N'ANSI nulls', N'false'
    GOexec sp_dboption N'wjw', N'concat null yields null', N'false'
    GOexec sp_dboption N'wjw', N'cursor close on commit', N'false'
    GOexec sp_dboption N'wjw', N'default to local cursor', N'false'
    GOexec sp_dboption N'wjw', N'quoted identifier', N'false'
    GOexec sp_dboption N'wjw', N'ANSI warnings', N'false'
    GOexec sp_dboption N'wjw', N'auto create statistics', N'true'
    GOexec sp_dboption N'wjw', N'auto update statistics', N'true'
    GOif( ( (@@microsoftversion / power(2, 24) = 8) and (@@microsoftversion & 0xffff >= 724) ) or ( (@@microsoftversion / power(2, 24) = 7) and (@@microsoftversion & 0xffff >= 1082) ) )
    exec sp_dboption N'wjw', N'db chaining', N'false'
    GOuse [wjw]
    GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[wujianwei]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[wujianwei]
    GOCREATE TABLE [dbo].[wujianwei] (
    [a] [datetime] NULL ,
    [b] [decimal](18, 0) NULL ,
    [c] [float] NULL ,
    [d] [image] NULL ,
    [e] [int] NULL ,
    [f] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL 
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO CREATE  CLUSTERED  INDEX [cxjjj] ON [dbo].[wujianwei]([a], [b], [c], [e], [f]) ON [PRIMARY]
    GO
      
     
    Top 
     
     回复人: hillmanweb(山人) ( ) 信誉:100  2003-03-05 21:58:00  得分:0 
     
     
      建议:如果你懒得去写建库的Sql的话,可以在你机子上建好库后,用SqlServer企业管理器来进行生成Sql脚本,对着任意一个库点击右键,从弹出菜单选择“所有任务”—>“生成Sql脚本”,具体请参考联机丛书。然后用楼上的方法进行调用即可。
      
      

  3.   

    我想你们都误会了我的意思,用你们的方法都可以建库,但有一个问题是,我想让用户指定数据库名称,比如放一个textbox,而调用.sql只能建.sql里确定了名称的数据库,用ado可以建库吗?
      

  4.   

    Before using the following statement, you should add the oledb reference as follows:
    menu(project)->reference->Microsoft Activex Data objects 2.5 library (add)Dim cn As ADODB.Connection
        Dim cmd As ADODB.Command
        Dim strCn As String
        Dim strDb As String
        Dim strSql As String
        
        
        Set cn = New ADODB.Connection
        strDb = Trim(txtdb) 'txtdb is a textbox control.
        strCn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=FUJITSU-DEMO"
        cn.Open strCn
        
        Set cmd = New ADODB.Command
        Set cmd.ActiveConnection = cn
        
        strSql = "CREATE DATABASE "
        strSql = strSql & strDb & " ON ( NAME = test_dat, FILENAME = 'c:\test.mdf', SIZE = 10,  MAXSIZE = 10, FILEGROWTH = 5 )"
        strSql = strSql & "LOG ON ( NAME = 'test_log',FILENAME = 'c:\test.ldf', SIZE = 5MB, MAXSIZE = 10MB, FILEGROWTH = 5MB )"
        
        cmd.CommandText = strSql
        cmd.CommandType = adCmdText
        cmd.CommandTimeout = 15
        cmd.Execute
        
        Set cmd = Nothing
        Set cn = Nothing
        
        strSql = "isql -S FUJITSU-DEMO -d "
        strSql = strSql & strDb & " -U sa -P -i createtable.sql -o createtable.out"
        Shell (strSql)
        You try the above method. The above statement:
    First with the oledb method to create the database.Then with the isql statement calling the sql file to create the table in the defined database
      

  5.   

    好了,成了,多谢 zxm954712(三绝剑)!