質問

C#でプログラムでデータベースを作成しようとしています。 SQL Server Management Studioから実行するときに正常に機能するデータベース作成のスクリプトがあります。ただし、C#アプリケーションから同じスクリプトを実行すると、次のエラーが発生します。

タイプ 'microsoft.sqlserver.management.common.executionFailureExceptionのタイプの未処理の例外は、microsoft.sqlserver.connectioninfo.dllで発生しました

なぜこれが起こっているのかについてのアイデアはありますか?

正しい解決策はありません

他のヒント

あなたにこれをすべて行うAC#フレーム作業があることをあなたに知らせるために。http://www.sharparchitecture.net/ OOモデルからDBとデータアクセスレイヤーを構築します。

public string createdatabase(文字列iPaddress、文字列ユーザー名、文字列パスワード、文字列db_filepath){

        Microsoft.SqlServer.Management.Smo.Server addDBserver = new     Microsoft.SqlServer.Management.Smo.Server(ipAddress);
        addDBserver.ConnectionContext.LoginSecure = false;
        addDBserver.ConnectionContext.Login = UserName;
        addDBserver.ConnectionContext.Password = Password;



        try
        {
            //*Crerate Databse*
            addDBserver.ConnectionContext.Connect();
            FileInfo filedb = new FileInfo(DB_filepath);
            string scriptdb = filedb.OpenText().ReadToEnd();
            string scriptdb1 = scriptdb.Replace("GO", Environment.NewLine);
            string scriptdb2 = scriptdb1.Replace("\r\nGO\r\n", "");
            addDBserver.ConnectionContext.ExecuteNonQuery(scriptdb2);
            addDBserver.ConnectionContext.Disconnect();
            string Msg;
                Msg = "db created successfully";
                return Msg;
            return true;


        }
        catch (Exception ex)
        {
       string Msg1 = "db notcreated successfully";
         return ex.Message;
           throw;
        }
    }
        //Database created Successfully
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top