Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > CreateDatabase at runtime
 

CreateDatabase at runtime

I have some sample code for creating a SQLExpress database. It is inside a click event for testing. The error is Create Database failed. Some file names listed could not be created. Check related errors. Create File encountered operating system operating system error 5 (error not found) while attempting to open or create the physical file "C:\MyDatabaseDate.mdf"

My operating system is Vista on this computer.

Here is my code
private void btnCreateDatabase_Click(object sender, EventArgs e)
        {
            String str;
            SqlConnection myConn = new SqlConnection("Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=master");

            str = "CREATE DATABASE MyDatabase ON PRIMARY " +
                "(NAME = MyDatabase_Data, " +
                "FILENAME = 'C:\\MyDatabaseData.mdf', " +
                "SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
                "LOG ON (NAME = MyDatabase_Log, " +
                "FILENAME = 'C:\\MyDatabaseLog.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();
                }
            }
        }
JohnFeeney  Tuesday, July 14, 2009 1:51 PM
With Vista, is it possible that you don't have rights to write a file to the root of your C drive?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, July 14, 2009 7:12 PM

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, July 21, 2009 6:43 AM

You can use google to search for other answers

Custom Search

More Threads

• ContextMenuStrip only show in header column of DataGridView
• DataGrid Disabling BindingNavigator Controls
• How to get data from Object Data Source in code
• datagridview help
• Accessing of SQLDataReader Object for particular cell information
• DataGridView drag and drop image
• BindingSource.Sort buffering
• Polling the directory when the new file is created in that directory.
• problems with binding to inherited class
• Customize ListView or Datagrid