Home All Groups Group Topic Archive Search About

Creating database via SMO

Author
31 Aug 2006 8:20 PM
Wernfried Schwenkner
I found some hints, how to create a database using SMO

The following:

Server server = new Server("localhost");
Database database = new Database(server, "TESTDATABASE");
FileGroup fileGroup = new FileGroup(database, "PRIMARY");
DataFile dataFile = new DataFile(fileGroup, "TESTDTABASE_DATA", "c:
\\TESTDATABASE_DATA.mdf");
dataFile.GrowthType = FileGrowthType.Percent;
dataFile.Growth = 10;
dataFile.Size = 4000;
fileGroup.Files.Add(dataFile);
database.FileGroups.Add(fileGroup);

LogFile logFile = new LogFile(database, "TESTDATABASE_LOG", "c:
\\TESTDATABASE_LOG.ldf");
logFile.GrowthType = FileGrowthType.Percent;
logFile.Growth = 10;
logFile.Size = 4000;
database.LogFiles.Add(logFile);
database.Create(false);

works. But if I replace "localhost" with ".\\SQLEXPRESS", I get the
exception message:

CREATE FILE encountered operating system error 5(Zugriff verweigert)
while attempting to open or create the physical file 'c:
\TESTDATABASE_DATA.mdf'.

On "locolhost" I'm running a SQLServer.

A similar error I have found here postet, the poster also tried with
".\\sqlexpress". The answer is still open, he was asked, if a CREATE
DATABASE works. I have tried this, with "localhost" and it also worked.
Here is the statement:

CREATE DATABASE testdb ON PRIMARY(Name=test_data, filename = 'C:
\\test_data.mdf', size=3,maxsize=5, filegrowth=10%)log on(name=test_log,
filename='C:\\test_log.ldf',size=3,maxsize=20,filegrowth=1)

To get it to run, I do not now, how the connection string is, whenn
connecting to the SQLExpress. This is the one for SQLServer:
Integrated Security=SSPI;" + "Initial Catalog=;" + "Data
Source=localhost;

Simply replacing "localhost" with ".\\sqlexpress" didn't work, got
connection error.

BTW, without specifying a different file location then the default,the
obove also works with SQLExpress.

Any ideas?

--
Sleepless in Berlin
W. Schwenkner

Author
1 Sep 2006 10:59 PM
Hugo Kornelis
On Thu, 31 Aug 2006 22:20:41 +0200, Wernfried Schwenkner wrote:

Show quote
>I found some hints, how to create a database using SMO
>
>The following:
>
>Server server = new Server("localhost");
>Database database = new Database(server, "TESTDATABASE");
>FileGroup fileGroup = new FileGroup(database, "PRIMARY");
>DataFile dataFile = new DataFile(fileGroup, "TESTDTABASE_DATA", "c:
>\\TESTDATABASE_DATA.mdf");
>dataFile.GrowthType = FileGrowthType.Percent;
>dataFile.Growth = 10;
>dataFile.Size = 4000;
>fileGroup.Files.Add(dataFile);
>database.FileGroups.Add(fileGroup);
>
>LogFile logFile = new LogFile(database, "TESTDATABASE_LOG", "c:
>\\TESTDATABASE_LOG.ldf");
>logFile.GrowthType = FileGrowthType.Percent;
>logFile.Growth = 10;
>logFile.Size = 4000;
>database.LogFiles.Add(logFile);
>database.Create(false);
>
>works. But if I replace "localhost" with ".\\SQLEXPRESS", I get the
>exception message:
>
>CREATE FILE encountered operating system error 5(Zugriff verweigert)
>while attempting to open or create the physical file 'c:
>\TESTDATABASE_DATA.mdf'.

Hi Wernfried,

Since Zugriff verweigert translates to Access denied, my bet would be
that SQL Server Express is configured to run under an account that has
no permission to create files in the root directory of your C: hard
drive. Thaht would allso explain why it suddenly works if yoou choose
another path - the account obviously dooes have file creation rights in
that directory.

--
Hugo Kornelis, SQL Server MVP
Author
2 Sep 2006 7:54 AM
Wernfried Schwenkner
In article <bnehf2p743qs33iel8156ipo7bsq3t1***@4ax.com>,
h***@perFact.REMOVETHIS.info.INVALID says...

> Since Zugriff verweigert translates to Access denied, my bet would be
> that SQL Server Express is configured to run under an account that has
> no permission to create files in the root directory of your C: hard
> drive. Thaht would allso explain why it suddenly works if yoou choose
> another path - the account obviously dooes have file creation rights in
> that directory.
>

Ok, this answer I can accept, but!

From within the Visual Studio I can create a SQLExpress database with
the wizzard in that directory. With my sample it doesn't work.

I thought, VS runs with the same account as my program?

--
Sleepless in Berlin
W. Schwenkner
Author
2 Sep 2006 10:41 AM
Erland Sommarskog
Wernfried Schwenkner (wernfried.nospam@schwenkner.de) writes:
> Ok, this answer I can accept, but!
>
> From within the Visual Studio I can create a SQLExpress database with
> the wizzard in that directory. With my sample it doesn't work.
>
> I thought, VS runs with the same account as my program?

Not really. First all, what matters for your program is not the account
the program runs under - it's the service account for the SQL Server
Express instance. Keep in mind that SQL Server is a server application.
You can use the SQL Server Configuration Manager to view and change the
service account for the SQL Service Service. (Log On tab under Properties.)

But why is then Visual Studio able to create a database through that
very same instance? The confusing answer is that it is not the same
instance. SQL Express has a feature that is not available in the other
editions of SQL Server: user instance. A user instance is spawned from
the Express instance, but runs in the context of the current user. And
when VS creates that database, it does so in a user instance. (At least
I think so. I have not looked that much into user instance, even less
into what Visual Studio is up to.)


--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

AddThis Social Bookmark Button