1

Model-First Entity Designer SQL ファイルを C# で実行する方法をいくつか試しましたが、うまくいきませんでした。ロードして読み取るすべてのコードを含む SQL ファイルがあります。

以下は私のコードです。実行されたすべてのコマンドでエラーが発生します。タイプ であるモデル コンテナの接続プロパティを介して接続を取得していることに注意してください。ただしDbConnection、それと関係があるかどうかはわかりません。

C# スクリプト:

var commandStrings = Regex.Split(Resources.DatabaseScript, "^\\s*GO\\s*$", RegexOptions.Multiline);

//container is my EDMX container.

container.Connection.Open();

var command = container.Connection.CreateCommand();
var transaction = container.Connection.BeginTransaction();
command.Connection = container.Connection;
command.Transaction = transaction;

foreach (string commandInput in commandStrings)
{
    var commandString = commandInput;
    if (commandString.Trim() != "")
    {
        Debug.Write("Executing SQL ... ");

        try
        {
            command.CommandText = commandString;
            command.Connection = container.Connection;
            command.CommandType = CommandType.Text;
            command.Prepare();
            command.ExecuteNonQuery();
            Debug.WriteLine("Success!");
        }
        catch (Exception exc)
        {
            Debug.WriteLine("Failed!");
            Debug.WriteLine("Exception: " + exc.Message);
            Debug.Write("Rolling back ... ");
            try
            {
                transaction.Rollback();
                Debug.WriteLine("Success!");
            } catch(Exception exce)
            {
                Debug.WriteLine("Exception: " + exce.Message);
            }
        }
        finally
        {
            Debug.WriteLine("SQL: " + commandString);
        }
    }
}
transaction.Commit();
container.Connection.Close();

受信したエラー。私が受け取っているエラーのいくつかは次のとおりです。

エラー 1:

IF OBJECT_ID(N'[dbo].[FK_UserStory]', 'F') IS NOT NULL
ALTER TABLE [dbo].[StorySet] DROP CONSTRAINT [FK_UserStory];

クエリ構文が無効です。識別子「OBJECT_ID」の近く、1 行目、4 列目。

エラー 2:

IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');

クエリ構文が無効です。識別子「SCHEMA_ID」の近く、1 行目、4 列目。

4

2 に答える 2

1

問題が具体的に何であるかはわかりませんが、例外処理とトランザクション制御が行われるコードの順序が少し奇妙であるため、以下で修正しました. それがあなたにとって何か違いがあるかどうか教えてください。

var commandStrings = Regex.Split(
    Resources.DatabaseScript,
    "^\\s*GO\\s*$",
    RegexOptions.Multiline | RegexOptions.Compiled);

// container is my EDMX container.

container.Connection.Open();
try
{
    using (var transaction = container.Connection.BeginTransaction())
    {
        try
        {
            foreach (var commandInput in commandStrings.Where(commandInput => !string.IsNullOrWhiteSpace(commandInput)))
            {
                Debug.Write("Executing SQL ... ");
                try
                {
                    using (var command = container.Connection.CreateCommand())
                    {
                        command.Connection = container.Connection;
                        command.Transaction = transaction;
                        command.CommandText = commandInput;
                        command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();
                    }

                    Debug.WriteLine("Success!");
                }
                finally
                {
                    Debug.WriteLine("SQL: " + commandInput);
                }
            }

            transaction.Commit();
        }
        catch (Exception exc)
        {
            Debug.WriteLine("Failed!");
            Debug.WriteLine("Exception: " + exc.Message);
            Debug.Write("Rolling back ... ");
            try
            {
                transaction.Rollback();
                Debug.WriteLine("Success!");
            }
            catch (Exception exce)
            {
                Debug.WriteLine("Exception: " + exce.Message);
            }
        }
    }
}
finally
{
    container.Connection.Close();
}

}

于 2012-05-02T21:42:36.393 に答える
0

問題を解決しました。私のコードはうまくいきました。

しかし、ModelContainer の接続を 経由で使用する代わりに、 を使用する必要container.Connectionがありました(container.Connection as EntityConnection).StoreConnection as SqlConnection

于 2012-05-09T17:37:58.747 に答える