程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> how to execute sql script in c#

how to execute sql script in c#

編輯:關於C#

A . Creating a Visual C# SMO project in Visual Studio.NET

Start Visual Studio 2008 (or Visual Studio 2005).

On the File menu, click New Project. The New Project dialog box appears.

In Project Types dialog box, select Visual C#, and then select Windows. In the Visual Studio Installed Templates pane, select Windows Application.

(Optional) In the Name field, type the name of the new application

Select the Visual C# application type. For the examples that follow, select Console Application.

On the Project menu, select Add Reference. The Add Reference dialog box appears.

URL:http://www.bianceng.cn/Programming/csharp/201410/45754.htm

Click Browse, locate the SMO assemblies in the C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\ folder, and then select the following files. These are the minimum files that are required to build an SMO application:

Microsoft.SqlServer.ConnectionInfo.dll

Microsoft.SqlServer.Smo.dll

Microsoft.SqlServer.Management.Sdk.Sfc.dll

Microsoft.SqlServer.SqlEnum.dll

Note Note

Use the Ctrl key to select more than one file.

Add any additional SMO assemblies that are required. For example, if you are specifically programming Service Broker, add the following assemblies:

Microsoft.SqlServer.ServiceBrokerEmum.dll

Click Open.

On the View menu, click Code.-Or-Select the Program1.cs [Design] Windows and double-click the windows form to show the code window.

In the code, before the namespace statement, type the following using statements to qualify the types in the SMO namespace:

using Microsoft.SqlServer.Management.Smo;  
using Microsoft.SqlServer.Management.Common;

SMO has various namespaces under Microsoft.SqlServer.Management.Smo, such as Microsoft.SqlServer.Management.Smo.Agent. Add these namespaces as they are required.

You can now add your SMO code.

B.c# code:

using Microsoft.SqlServer.Management.Smo;  
using Microsoft.SqlServer.Management.Common;  
  public void ExcSqlScript(string sql)  
        {  
            var conn = new SqlConnection(_connStr);  
            var server = new Server(new ServerConnection(conn));  
            server.ConnectionContext.ExecuteNonQuery(sql);  
        }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved