0

SQL Server 2008 R2 Express、.Net Framework 4.0、Visual Studio 2010

コマンド プロンプト アプリケーションから SQL スクリプトを実行しようとしています。サンプル
コードを見つけて、同じものを実装しようとしました。ただし、次の using ステートメントは認識されません。

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

アセンブリ参照がありませんか?

4

3 に答える 3

2

次の c# コードは、SMO (SQL Server Management Object) を使用して .sql ファイルから SQL クエリを読み取り、SQL サーバーで実行します。

 #region Using Directives

    using System.Configuration;
    using System.Data.SqlClient;
    using System.IO;
    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
    using System.Xml.Linq;
    using System;

    #endregion

    public sealed class DatabaseHandler
    {
        #region Properties

        /// <summary>
        /// Returns the Database Connection String
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return ConfigurationManager.AppSettings["DbConnectionString"];
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Reads the script conent from .sql file and execute on SQl Server
        /// </summary>
        /// <param name="scriptFilePath">.sql Script file path</param>
        /// <returns>Operation status <c>true: Success</c><c>false: Failed</c></returns>
        public bool ExecuteScript(string scriptFilePath)
        {
            try
            {
                bool isCreated = false;

                if (!string.IsNullOrWhiteSpace(scriptFilePath))
                {
                    FileInfo fileInfo = new FileInfo(scriptFilePath);

                    if (null != fileInfo)
                    {
                        //Holds the sql script as string
                        string scriptText = string.Empty;

                        using (StreamReader reader = fileInfo.OpenText())
                        {
                            if (null != reader)
                            {
                                scriptText = reader.ReadToEnd();
                            }
                        }

                        using (SqlConnection connection = new SqlConnection(ConnectionString))
                        {
                            Server sqlServer = new Server(new ServerConnection(connection));

                            if (null != sqlServer && null != sqlServer.ConnectionContext)
                            {
                                sqlServer.ConnectionContext.ExecuteNonQuery(scriptText);
                            }
                        }
                    }
                }

                isCreated = true;
                return isCreated;
            }
            catch (FileNotFoundException)
            {
                throw new FileNotFoundException("Unable to find" + scriptFilePath);
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion
    }
于 2013-06-26T15:23:27.840 に答える
1

2017 年になった今、もっと簡単な方法があります。

この NuGet パッケージを追加します: https://www.nuget.org/packages/Microsoft.SqlServer.SqlManagementObjects

于 2017-09-17T03:02:42.843 に答える