Microsoft KB Archive/320628

From BetaArchive Wiki

Article ID: 320628

Article Last Modified on 5/13/2007



APPLIES TO

  • Microsoft ADO.NET 1.0
  • Microsoft ADO.NET 1.1
  • Microsoft Visual J# .NET 2003 Standard Edition
  • Microsoft Visual J# .NET 2003 Standard Edition



This article was previously published under Q320628


For a Microsoft Visual C++ .NET version of this article, see 307402.
For a Microsoft Visual C# .NET version of this article, see 307283.
For a Microsoft Visual Basic .NET version of this article, see 305079.

IN THIS TASK

SUMMARY

REFERENCES

SUMMARY

Programmers must frequently create databases programmatically. This step-by-step article describes how to use ADO.NET and Visual J# to programmatically create a Microsoft SQL Server database.

back to the top

Steps to Create the Sample

  1. Create a new Visual J# Windows Application project. By default, Form1 is added to the project.
  2. Place a Command button on Form1, and then change its Name property to btnCreateDatabase and its Text property to Create Database.
  3. Paste the following line of code below the other import statements in Form1.jsl:

    import System.Data.SqlClient.*;
                        
  4. Paste the following code after the Windows Form Designer generated code region:

    private void btnCreateDatabase_Click (System.Object sender, System.EventArgs e)
    {
        SqlDataReader reader;
        String str;
        SqlConnection myConn;
        SqlCommand myCommand;
            
        try
        {
    
        myConn = new SqlConnection("Server=localhost;" +
                     "Integrated security=SSPI;" +
                     "database=master");
        
        str = "CREATE DATABASE MyDatabase ON PRIMARY" +
        " (NAME = MyDatabase_Data," +
        " FILENAME = 'C:\\MyDatabaseData.mdf'," +
        " SIZE = 2MB," +
        " MAXSIZE = 10MB," +
        " FILEGROWTH = 10%)" +
        " LOG ON" +
        " (NAME = MyDatabase_Log," +
        " FILENAME = 'C:\\MyDatabaseLog.ldf'," +
        " SIZE = 1MB," +
        " MAXSIZE = 5MB," +
        " FILEGROWTH = 10%)";
            
        myCommand= new SqlCommand(str, myConn);
    
            myConn.Open();
            reader = myCommand.ExecuteReader();
            if (reader != null)
            {
            reader.Close();
            if(myConn.get_State() == ConnectionState.Open)
                    {
                myConn.Close();
                }
                    MessageBox.Show("Database is created successfully",
              "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
             }
             catch(Exception ex)
        {
            MessageBox.Show(ex.fillInStackTrace().toString());
        }
    }
                        
  5. Change the connection string to point to the computer that is running SQL Server, and then make sure that the Database argument is either blank or is set to Master.
  6. Press F5 or CTRL+F5 to run the project, and then click Create Database.

back to the top

Additional Notes

  • This code creates a custom database with specific properties.
  • The folder that will hold the created .mdf and .ldf files must already exist before you run the code or an exception will be generated.
  • If you want to create a database that is similar to the SQL Server Model database and that is in the default location, change the str variable in the code to the following:

    str = "CREATE DATABASE MyDatabase"
                        

back to the top

REFERENCES

For more information about the CREATE DATABASE Transact-SQL command, see the SQL Server Books Online or MSDN Online Library:

For more information about ADO.NET objects and syntax, see the Microsoft .NET Framework SDK 1.0 Documentation or MSDN Online Library:

For more general information about ADO.NET, visit the following MSDN newsgroup:

For more information about Visual J# .NET, visit the following MSDN newsgroup:

back to the top

Keywords: kbhowtomaster KB320628