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.










Son Yorumlar