Hi guys i am a student try ing to create a database within a WPF class in c#. Would appreciate any advice.
regards,
Kieran
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pravin MorePosted Nov 9, 2011, 4:55 AM
here is code for creating database programmatically.......
Note:- change coonection string etc. setting according to yours....
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;
}
Thanks.
Pravin.
Mark this post Accepted Answer, if it helps you.
Mamta MPosted Nov 9, 2011, 4:44 AM