use SqlCommand to run something like"EXEC master..xp_cmdshell 'osql -SYourServerName -UYourLoginName -PYourPassword -ic:\MySQl.sql -n'"
思归大虾: 怎样用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 ”的字符串儿吗?? 如果可以我应该怎样做? 谢谢!!
可以看一下这个例子 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
怎样用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
”的字符串儿吗??
如果可以我应该怎样做?
谢谢!!
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