0

I Work on C# Project (WinForm)

I Install Sql Server 2008 Express On Client PC.

in Start Of My Program Must Create a Database. So I Use This Code For Create a Database:

string sqlCreateDBQuery;

            SqlConnection tmpConn = new SqlConnection(@"SERVER =.\SQLEXPRESS; Trusted_Connection = yes;DATABASE = master;");

            sqlCreateDBQuery = " CREATE DATABASE "
                               + DatabaseName
                               + " ON PRIMARY "
                               + " (NAME = " + DatabaseName + ", "
                               + " FILENAME = '" + @"C:" + @"\" + DatabaseName + ".mdf" + "', "
                               + " SIZE = 3MB,"
                               + " FILEGROWTH = " + "10%" + ") "
                               + " LOG ON (NAME =" + "MyDatabase_Log" + ", "
                               + " FILENAME = '" + @"C:" + @"\" + DatabaseName + "_log.ldf" + "', "
                               + " SIZE = 1MB, "
                               + " FILEGROWTH = " + "10%" + ") ";
            sqlCreateDBQuery = Coomand;

            SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);
            try
            {
                tmpConn.Open();
                MessageBox.Show(sqlCreateDBQuery);
                myCommand.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                tmpConn.Close();
            }
            return;

But When My Program Run I See Following Error

enter image description here

What is My Problem?

mehdi
  • 59
  • 4
  • 10
  • See this: http://www.codeproject.com/Articles/10213/Create-a-SQL-Server-Database-Using-C – NoChance Jul 11 '12 at 15:03
  • Thanks My friend. But Doesn't Work. – mehdi Jul 11 '12 at 15:27
  • In you code, avoid creating the files in root directory C (see error message). Use a sub directory you can read/write to. As per the link, What errors are you getting? Are you running the app as admin? Can you connect to DB? – NoChance Jul 11 '12 at 15:29
  • Thanks , it works. i create db in c:\aaa\ali.mdf and work --- Why this error Happend . How Can Understand what Directory is Safe to Make DB? – mehdi Jul 11 '12 at 15:37
  • The root directory is protected for some Windows versions. Most applications don't have enough auth. to write to C:\, so without acquiring privs, you can't create files there. – NoChance Jul 11 '12 at 15:43
  • This is what I recommended, not leaving it at the root, but why in an arbitrary aaa folder? Malpractice. Put in in the same deployment folder as your project. – GrayFox374 Jul 11 '12 at 15:53
  • Do you Know How Can acquiring privs For My Program? – mehdi Jul 11 '12 at 15:55
  • @GrayFox374 I know My Friend. but in First I Try to Create DB File In Program Files And My Installation Folder but See This Message Again. I,m Confusing because Can,t Create DB File Even in My Installation Folder. – mehdi Jul 11 '12 at 16:11
  • What is the path of your installation folder? – NoChance Jul 11 '12 at 18:38
  • I:/Programs Files/Default Company Name/Setup/Data/Default/LH_Default.mdf – mehdi Jul 11 '12 at 18:52
  • Right click on the folder and see the security tab, identify if you have write access to the folder. – NoChance Jul 11 '12 at 19:28
  • Answers go below, not in the body of the question. –  Jul 28 '12 at 20:16

1 Answers1

0

It looks from the error message that you don't have permissions to create your mdf file on the root. Truth be told, you should not have put it there anyway, as that is much too exposed. Put it in your application's folder, or somewhere obscure where it won't be accidentally deleted.

I also think that you should be running this as a install script for the initial setup, rather than in your code. I am a big proponent of keeping sql out of code as much as possible, but to each his own. You say you set up the SQL Server DB, you should use the tools at your disposal to make this easy for you. I had a book from Wrox that served me well, here is the link: http://www.wrox.com/WileyCDA/WroxTitle/Wrox-s-SQL-Server-2005-Express-Edition-Starter-Kit.productCd-0764589237.html -Amazon has it for $2 - http://www.amazon.com/Server-Express-Edition-Starter-Programmer/dp/B006TQYC8U/ref=sr_1_1?ie=UTF8&qid=1342019983&sr=8-1&keywords=Wrox%27s+SQL+Server+2005+Express+Edition+Starter+Kit

Also:

http://msdn.microsoft.com/en-us/library/bb264562(v=sql.90).aspx

GrayFox374
  • 1,742
  • 9
  • 13
  • I install Sql server 2008 Express From Setup Version (That Visual Studio Own Make it) and this Sql server Doesn't Have "Sql Server Management Studio". And When Run My Program On My PC. it,s Work Well. but When Install And Run On Client PC , Has Error. I think Sql server that I install On Client PC is Raw And Doesn,t Have Any user And Pass (Instance). – mehdi Jul 11 '12 at 15:34