C# ile Yeni Sql Server Database Oluşturma

 private void CreateDatabase(DatabaseParam DBParam)
{
    System.Data.SqlClient.SqlConnection tmpConn;
    string sqlCreateDBQuery;
    tmpConn = new SqlConnection();
    tmpConn.ConnectionString = "SERVER = " + DBParam.ServerName +
                         "; DATABASE = master; User ID = sa; Pwd = sa";
    sqlCreateDBQuery = " CREATE DATABASE "
                       + DBParam.DatabaseName
                       + " ON PRIMARY "
                       + " (NAME = " + DBParam.DataFileName +", "
                       + " FILENAME = '" + DBParam.DataPathName +"', "
                       + " SIZE = 2MB,"
                       + " FILEGROWTH =" + DBParam.DataFileGrowth +") "
                       + " LOG ON (NAME =" + DBParam.LogFileName +", "
                       + " FILENAME = '" + DBParam.LogPathName + "', "
                       + " SIZE = 1MB, "
                       + " FILEGROWTH =" + DBParam.LogFileGrowth +") ";
     SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);
     try
     {
         tmpConn.Open();
         MessageBox.Show(sqlCreateDBQuery);
         myCommand.ExecuteNonQuery();
         MessageBox.Show("Database has been created successfully!",
                           "Create Database", MessageBoxButtons.OK,
                                       MessageBoxIcon.Information);
      }
     catch (System.Exception ex)
     {
         MessageBox.Show(ex.ToString(), "Create Database",
                                     MessageBoxButtons.OK,
                              MessageBoxIcon.Information);
     }
     finally
     {
         tmpConn.Close();
     }
     return;
}

örnek uygulamaya buradan erişebilirsiniz.

DatabaseParam struct yapısı şu şekilde.

 struct DatabaseParam
		{
			public string	ServerName;
			public string	DatabaseName;
			//Data file parameters
			public string	DataFileName;
			public string	DataPathName;
			public string	DataFileSize;
			public string	DataFileGrowth;
			//Log file parameters
			public string	LogFileName;
			public string	LogPathName;
			public string	LogFileSize;
			public string	LogFileGrowth;
		}

Örnek kodda servername kullanılmıyor. ancak siz servername i kendinize göre değiştirmelisiniz.

Oca 24th, 2012 | Kategoriler C# Dersleri | Toplam Okunma=17.239
İlk yorumu siz yazın.

Yorum Yazın

XHTML: İzin verilen HTML kodları: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>