实现这个功能有两个方法。
1.用SQLDMO这个Com。2.用SQLCommand来实现。
  用filestream把脚本文件的sql脚本读入。
  把所有读入的内容用字符变量保存。
  定义一个连接对象及一个命令对象。
  执行命令对象。

解决方案 »

  1.   

    use SqlCommand to run something like"EXEC master..xp_cmdshell 'osql -SYourServerName -UYourLoginName -PYourPassword -ic:\MySQl.sql -n'"
      

  2.   

    思归大虾:
    怎样用SQLCommand 来运行
    "EXEC master..xp_cmdshell 'osql -SYourServerName -UYourLoginName -PYourPassword -ic:\MySQl.sql -n'" 这句话呢?
    请详细一点告诉小弟
    谢谢!!baitianhai大虾:
    SQLCommand能运行类似

    IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'master')
    DROP DATABASE [master]
    GOCREATE DATABASE [master]  ON (NAME = N'master', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL\data\master.mdf' , SIZE = 13, FILEGROWTH = 10%) LOG ON (NAME = N'mastlog', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL\data\mastlog.ldf' , SIZE = 1, FILEGROWTH = 10%)
     COLLATE Chinese_PRC_CI_AS
    GOexec sp_dboption N'master', N'autoclose', N'false'
    GO
    ”的字符串儿吗??
    如果可以我应该怎样做?
    谢谢!!
      

  3.   

    可以看一下这个例子
    Private Function GetSql(ByVal Name As String) As String
       Try      ' Get the current assembly.
          Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()      ' Resources are named using a fully qualified name.
          Dim strm As Stream = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name)      ' Read the contents of the embedded file.
          Dim reader As StreamReader = New StreamReader(strm)
          Return reader.ReadToEnd()
       Catch ex As Exception
          MsgBox("In GetSQL: " & ex.Message)
          Throw ex
       End TryEnd FunctionPrivate Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As String)
       Dim Command As New SqlClient.SqlCommand(Sql, sqlConnection1)   Command.Connection.Open()
       Command.Connection.ChangeDatabase(DatabaseName)
       Try
          Command.ExecuteNonQuery()
       Finally
          ' Finally, blocks are a great way to ensure that the connection 
          ' is always closed.
          Command.Connection.Close()
       End Try
    End SubProtected Sub AddDBTable(ByVal strDBName As String)
       Try
          ' Create the database.
          ExecuteSql("master", "CREATE DATABASE " + strDBName)      ' Create the tables.
          ExecuteSql(strDBName, GetSql("sql.txt"))   Catch ex As Exception
           ' Report any errors and abort.
           MsgBox("In exception handler: " & ex.Message)
           Throw ex
       End Try
    End Sub