The situation is this, I create the following code from a C # application

CreateDB_SQL_command = "CREATE DATABASE " + "ExpData" + " ON PRIMARY " + "(NAME = Exp_Data_" + @DbName + ", " + @"FILENAME = '" + @DbFileName + "', " + "SIZE = 100MB, MAXSIZE = 10GB, FILEGROWTH = 10%) " + "LOG ON (NAME = " + @DbLogName + ", " + @"FILENAME = '" + @DbLogFileName + "', " + "SIZE = 100MB, " + "MAXSIZE = 5GB, " + "FILEGROWTH = 15%)"; SqlCommand myCommand = new SqlCommand(CreateDB_SQL_command, myConn); try { myConn.Open(); myCommand.ExecuteNonQuery(); } 

The base is created, everything is fine, from ManagmenStudio is perfectly controlled, but when you try to open it

 switch (open_db_file.ShowDialog()) { case System.Windows.Forms.DialogResult.OK: if (open_db_file.FileNames.Count() > 1) { MessageBox.Show("необходимо выбрать только 1 файл", "ошибка"); return; } FilePath = open_db_file.FileName; break; case System.Windows.Forms.DialogResult.Cancel: return; } str_build = new SqlConnectionStringBuilder(); str_build.DataSource = ".\\SQLEXPRESS"; str_build.IntegratedSecurity = true; str_build.InitialCatalog = FilePath; str_build.MultipleActiveResultSets = true; using (ExpDataContext _db = new ExpDataContext(str_build.ToString())) { //....... } 

When I select a file in OpenFileDialog I get a MessageBox which says that I do not have permission to access this file. The problem is treated if the hands of this file register the access rights from the current windows user, through the explorer. Actually the question is, is it possible in some way to configure SQL Server or so modify the database creation script so that the current user, or at least everyone had the right to access the .mdf file? I read the manual from msdn, but did not find the answer there.

  • Himself faced with this. Before SQL Server 2012, there were no problems. - Oleg53
  • I have it worth it. SQL Server 2012 Express - Mikhail Tatarintsev
  • I didn't solve this problem programmatically. Customers, in the description, I propose to do everything "hands". Now busy with others. I would be grateful if you find a solution. - Oleg53
  • friend advised by means of winAPI when creating (after) myCommand.ExecuteNonQuery (); change file permissions. Now I’m going to decide on how I’ll write the working code - I’ll post the answer to the question - Mikhail Tatarintsev
  • It's clear. I'm embarrassed that there were no problems before SQL Server 2012. And in general, with very little experience with this version, I didn’t like it - I can’t directly open, created earlier for 2000 databases, the absence of SQL-DMO, etc. Maybe it is because of laziness? Until recently, at small volumes, I preferred compact MSDE. Especially if the database is local. When working with local, the problem is to get the SQL server name. I hardly got it on Win7, and this code no longer works on Win8 ... - Oleg53

1 answer 1

And why do you even need access to the database file (via explorer)? The application works with the database through SQL Server, all he needs to know is the name of the database - InitialCatalog . Which may not coincide with the file name!

Enter InitialCatalog = "master". Open the connection and execute the query

 SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb'); 

The result is a list of all non-system databases - load into the combo box and show it to the user. Substitute the user-selected value as InitialCatalog in your code, instead of FilePath .