2012-01-25 75 views
3

剧本我都在SQL Server 2008中.sql文件,我想执行对我的数据库的数据库脚本。相反,这是一个手动练习,我想将它自动化。我有3个选项可用于我,批处理文件,PowerShell或C#。我执行此操作的方式会受到我下一个问题的影响 - 我想执行脚本,但是如果脚本因任何原因无法执行,请在代码中注意这一点?这将成为一组常规安装步骤的一部分,因此如果脚本无法正确执行,希望安装停止。执行对SQL Server数据库

+0

任何你不使用SQL代理的原因? –

+0

安装过程的其余部分如何处理? – NotMe

+0

它目前是一个处理安装过程的批处理文件,但会作为对此问题的回应的一部分进行更改。 – amateur

回答

1

运行它作为SQL代理作业,你可以设置SQL代理如果脚本未能向您发送警报。

+0

我希望它自动化,作为安装过程的一部分,正如问题所述。 – amateur

+0

@amateur,你可以自动化SQL代理作业。可以使用一系列系统特效“sp_add_job”等创建它们。 – Ben

-1

您也可以在存储过程中收拾这个,有使用一些错误处理。

+0

这不能作为安装过程的一部分包含在内。 – amateur

-1
  • 通过sqlcmd.exe引导程序运行 - 更难处理错误
  • 通过自己的自定义应用程序中运行 - 处理错误,并发出警报
+0

它是安装过程的一部分,所以这些选项都不适用。 – amateur

+0

更正的答案 –

0

以下是通过安装在c#

代码在C#中自动执行脚本的进程

using (System.Data.SqlClient.SqlConnection con = new SqlConnection("YourConnection string")) { 
    con.Open(); 
    SqlCommand cmd = new SqlCommand(); 
    string expression = "Parameter value"; 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "Your Stored Procedure"; 
    cmd.Parameters.Add("Your Parameter Name", SqlDbType.VarChar).Value = expression; 
    cmd.Connection = con; 
    using (IDataReader dr = cmd.ExecuteReader()) { 
     if (dr.Read()) { 
     } 
    } 
} 

在你的项目添加一个安装程序类参考=>Installer class

You can find below mentioned events for the installer process. 
1. Install 
2. Commit 
3. Rollback 
4. UNInstall 

写下来的数据库连接和脚本执行代码在上述事件中。如果安装程序未能执行脚本,则异常将到来,并且控件将自动移至回滚事件。所以最后回滚事件会告诉用户由于执行脚本失败而无法安装...

0

您可以尝试SQL Server管理对象(SMO - http://msdn.microsoft.com/en-us/library/ms162169.aspx),这应该在您的方案中起作用,在安装过程中执行sql脚本。

在这个简短的代码,你可以看到如何连接到SQL Server实例,这里使用Windows身份验证,但你也可以通过使用您的凭据提供connection.Login和connection.Password使用SQL身份验证。 之后的事件处理程序设置可以在(SERVERMESSAGEInfoMessage)和火灾后(StatementExecuted)脚本执行。同时使用SERVERMESSAGEInfoMessage这里是矫枉过正,因为SERVERMESSAGE也将显示提示性消息(在SQL错误的严重性< 10),但它是很好的看到它的工作方式。

在此示例中,textBox1.Text包含一个用此行执行的T-SQL脚本: server.ConnectionContext.ExecuteNonQuery(textBox1.Text); 执行语句包围一个try ... catch执行过程中捕捉任何错误。

private void button1_Click(object sender, EventArgs e) 
    { 
     ServerConnection connection = new ServerConnection("stjepan-lap"); 
     connection.LoginSecure = true; 
     Server server = new Server(connection); 
     server.ConnectionContext.InfoMessage += new SqlInfoMessageEventHandler(ConnectionContext_InfoMessage); 
     server.ConnectionContext.StatementExecuted += new StatementEventHandler(ConnectionContext_StatementExecuted); 
     server.ConnectionContext.ServerMessage += new ServerMessageEventHandler(ConnectionContext_ServerMessage); 

     //Executes T-Sql script 
     try 
     { 
      server.ConnectionContext.ExecuteNonQuery(textBox1.Text); 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex.Message); 
      if (ex.InnerException != null) 
       Debug.WriteLine(ex.InnerException.Message); 
     } 

     server.ConnectionContext.Disconnect(); 
    } 

    void ConnectionContext_ServerMessage(object sender, ServerMessageEventArgs e) 
    { 
     Debug.WriteLine(e.Error); 
    } 

    void ConnectionContext_StatementExecuted(object sender, StatementEventArgs e) 
    { 
     Debug.WriteLine(e.SqlStatement); 
    } 

    void ConnectionContext_InfoMessage(object sender, SqlInfoMessageEventArgs e) 
    { 
     Debug.WriteLine(e.Message); 
    } 

您应该引用SMO dll文件,并把相应的usings在你的代码文件

Microsoft.SqlServer.Smo.dll 
Microsoft.SqlServer.ConnectionInfo.dll 
Microsoft.SqlServer.Management.Sdk.Sfc.dll 
.... 
using System; 
using Microsoft.SqlServer.Management.Smo; 
using Microsoft.SqlServer.Management.Common; 
0

一批比较容易,我认为(但这是一个喜好)

runsqlscr.cmd

@echo off 
REM :: building your script below, not needed if already created 
REM echo Select * from PutTableHere where > %temp%\tmpsql.sql 
REM echo "put rest of sql script here one line at a time" >> %temp%\tmpsql.sql 
REM echo go >> %temp%\tmpsql.sql 
REM echo. 
REM echo script written 
REM echo. 
REM echo ready to execute script 
REM :: delete pause if needed 
REM pause 
REM :: the actual command needed in script 
REM sqlcmd -U thedbuser -P thepasswd -d thedbname -o resultsofscript.txt < %temp%\tmpsql.sql 
REM echo All done, displaying results 
REM type resultofscript.txt 
REM echo. 

如果脚本已经完成,您可以跳过构建脚本,只需更改%temp%tmpsql.sql 到脚本的位置和名称。

在SQLCMD线

,你需要把自己的价值观给“*”上述

用于测试,所有你需要为你的安装脚本是SQLCMD线。

快乐配料

相关问题