简体   繁体   English

动态创建.mdf / .sdf数据库

[英]Create .mdf/.sdf database dynamically

How can I with "code" create a new .mdf/.sdf database? 如何使用“代码”创建一个新的.mdf / .sdf数据库?

I've tried this: http://support.microsoft.com/kb/307283 我试过这个: http//support.microsoft.com/kb/307283

All it does is fail on the ConnectionString. 它只是在ConnectionString上失败了。 Since I have no connection to a file that exists before I create it, how can I only connect to the SQL Express Server just to create a mdf/sdf database? 由于我在创建之前没有连接到存在的文件,我怎么才能只创建一个mdf / sdf数据库来连接到SQL Express Server?

I want to be able to just connect to the server and create the file, from there it probably will be easier to create the tables and such. 我希望能够只连接到服务器并创建文件,从那里可能更容易创建表等。

Any suggestions? 有什么建议么?

public static void CreateSqlDatabase(string filename)
{
    string databaseName = System.IO.Path.GetFileNameWithoutExtension(filename);
    using (var connection = new System.Data.SqlClient.SqlConnection(
        "Data Source=.\\sqlexpress;Initial Catalog=tempdb; Integrated Security=true;User Instance=True;"))
    {
        connection.Open();
        using (var command = connection.CreateCommand())
        {
            command.CommandText =
                String.Format("CREATE DATABASE {0} ON PRIMARY (NAME={0}, FILENAME='{1}')", databaseName, filename);
            command.ExecuteNonQuery();

            command.CommandText =
                String.Format("EXEC sp_detach_db '{0}', 'true'", databaseName);
            command.ExecuteNonQuery();
        }
    }
}

Change Catalog=tempdb to Catalog=master , its good worked Catalog=tempdb更改为Catalog=master ,它的效果很好

Sample use: 样品用途:

var filename = System.IO.Path.Combine("D:\\", "testdb.mdf");
if (!System.IO.File.Exists(filename))
{
    CreateSqlDatabase(filename);
}

关于.sdf文件(SQL Server CE),您可以使用SqlCeEngine类创建新数据库,如本MSDN文章中所述

Make sure you have a valid connection string. 确保您有一个有效的连接字符串。

The database/catalog that you need must be set to a valid database, usually this can be the "master" which is always available and since you will be using master to create a database. 您需要的数据库/目录必须设置为有效的数据库,通常这可以是始终可用的“主”,因为您将使用master来创建数据库。

If you need to create a database from scratch programmatically i normal go into the SQL Server Management Studio and create it through the gui in a first step. 如果您需要以编程方式从头开始创建数据库,我通常会进入SQL Server Management Studio并在第一步中通过gui创建它。 But instead of clicking on the OK button in the bottom right, i click on the Script button in the top toolbar. 但是,不是单击右下方的“确定”按钮,而是单击顶部工具栏中的“脚本”按钮。 This will give me a complete sql script for creating the database i'd like to have. 这将为我提供一个完整的sql脚本,用于创建我想拥有的数据库。 Then i can alter the script and change the parts i'd like dynamically. 然后我可以改变脚本并动态更改我想要的部分。

I suppose the problem is in the ConnectionString. 我想这个问题在ConnectionString。 It should point to the valid instance of the master db (as in the article you refer to). 它应该指向数据库的有效实例(如您引用的文章中所述)。 Make sure it is correct, and it should work. 确保它是正确的,它应该工作。

Use a connectionString with InitialCatalog = master . 将connectionString与InitialCatalog = master Since only master has default access to create a database. 由于只有master具有创建数据库的默认访问权限。

Create .sdf database 创建.sdf数据库

using System.Data.SqlServerCe;
using System.IO;
  string folderPath="D:\\Compact_DB"
  string connectionString;
  string fileName =folderPath+"\\School.sdf";
  string password = "12345";

  if (File.Exists(fileName))
  {
    File.Delete(fileName);
  }

  connectionString = string.Format("DataSource=\"{0}\"; Password='{1}'",    fileName, password);
  SqlCeEngine obj_ceEngine = new SqlCeEngine(connectionString);
  obj_ceEngine.CreateDatabase();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM