以编程方式创建数据库

发布于 2024-09-14 21:19:28 字数 301 浏览 8 评论 0原文

我正在尝试用 C# 以编程方式创建数据库。我有用于数据库创建的脚本,当我从 SQL Server Management Studio 运行它们时,它们工作得很好。但是,当我从 C# 应用程序运行相同的脚本时,会出现以下错误:

Microsoft.SqlServer.ConnectionInfo.dll 中发生类型为“Microsoft.SqlServer.Management.Common.ExecutionFailureException”的未处理异常

关于为什么会发生这种情况有什么想法吗?

I am trying to create a database programmatically in C#. I have scripts for database creation which work fine when I run them from SQL Server Management Studio. However, when I run the same scripts from my C# application, the following error occurs:

An unhandled exception of type 'Microsoft.SqlServer.Management.Common.ExecutionFailureException' occurred in Microsoft.SqlServer.ConnectionInfo.dll

Any ideas as to why this might be happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2024-09-21 21:19:28

只是为了让您知道有一个 ac# 框架可以为您完成这一切。
http://www.sharparchitecture.net/ 它将从面向对象构建您的数据库和数据访问层模型。

Just to let you know there is a c# frame work which will do all this for you.
http://www.sharparchitecture.net/ it will build your DB and your data access layer from a OO model.

£噩梦荏苒 2024-09-21 21:19:28

公共字符串CreateDataBase(字符串ip地址,字符串用户名,字符串密码,字符串DB_文件路径)
{

        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

public string CreateDataBase(string ipAddress, string UserName, string Password,string 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文