【问题标题】:How can I create a SQL Server database file (.mdf) programmatically from C#?如何从 C# 以编程方式创建 SQL Server 数据库文件 (.mdf)?
【发布时间】:2016-10-19 13:49:47
【问题描述】:

我是新来的。最近,我的老板命令我从一个 Winforms 应用程序制作一个 SQL Server 数据库。我正在尝试以编程方式创建数据库。但每次我收到一条错误消息。

是否可以通过编程方式创建 SQL Server 数据库文件 (.mdf)?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace mdf_creator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           InitializeComponent();
        }

        private void btnCreateDatabase_Click(object sender, EventArgs e)
        {
            String str;
            SqlConnection myConn = new SqlConnection("Data Source =(LocalDB)\\MSSQLLocalDB; Integrated security =SSPI;database=master");
        str = "CREATE DATABASE ss ON PRIMARY " +
            "(NAME = ss_Data, " +
            "FILENAME = 'D:\\ss.mdf', " +
            "SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
            "LOG ON (NAME = ss_Log, " +
            "FILENAME = 'D:\\ss.ldf', " +
            "SIZE = 1MB, " +
            "MAXSIZE = 5MB, " +
            "FILEGROWTH = 10%)";

        SqlCommand myCommand = new SqlCommand(str, myConn);

        try
        {
            myConn.Open();
            myCommand.ExecuteNonQuery();
            MessageBox.Show("DataBase is Created Successfully", "MyProgram",     MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.ToString(), "MyProgram", 
MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        finally
        {
                if (myConn.State == ConnectionState.Open)
                {
                    myConn.Close();
                }
        }
    }
}

我的代码哪里出了问题?

【问题讨论】:

  • 拒绝访问非常有用。您没有权限在您尝试创建文件的地方创建文件。
  • 我将此评论作为答案发布,请参考
  • 我也使用了另一个位置。但它显示相同的错误。
  • 您似乎无权访问您的 D:\ 驱动器。您的错误消息显示“访问被拒绝”。
  • 虽然我以管理员身份运行 Visual Studio,但同样的问题。

标签: c# sql-server database winforms


【解决方案1】:

我建议首先以管理员身份运行 Visual Studio。右键单击 Visual Studio 图标并“以管理员身份运行”。

我还建议在这里实施答案:How to request administrator permissions when the program starts?

这样您的应用程序将需要在运行它的机器上具有管理员访问权限,如果它正在创建/删除文件,通常需要此权限。

编辑:如果您在 Visual Studio 以管理员身份运行时仍然无法访问该驱动器,则可能是 Window 对该驱动器的权限,而不是您的项目有任何问题。

【讨论】:

    【解决方案2】:

    是的。

    您可以使用SQL Server Managments Objects

    //Connect to the local, default instance of SQL Server.   
    Server srv = new Server();  
    
    // Define a Database object variable by supplying the server and the 
    // database name arguments in the constructor.   
    Database db = new Database(srv, "Test_SMO_Database");  
    
    //Create the database on the instance of SQL Server.   
    db.Create();  
    
    //Reference the database and display the date when it was created.   
    db = srv.Databases["Test_SMO_Database"];  
    Console.WriteLine(db.CreateDate);  
    

    【讨论】:

    • 我对编程很陌生。我不明白你的回答先生。
    【解决方案3】:

    错误消息表明 SQL 服务器无法在“D:”驱动器上创建文件。确保运行 SQL 服务器的用户具有对该驱动器的必要映射和访问权限。

    【讨论】:

    • 我试图将数据库保存到驱动器的其余部分。但它显示相同的错误消息。
    • 需要权限的不是你。它是 SQL 服务器。查看您的 services.msc 并查看 SQL Server 以什么用户身份运行。那是需要权限的用户。
    猜你喜欢
    • 2010-10-19
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多