Generate a Database in Runtime from Script File


Here I post the coding of Creation of Database in Runtime from Script File.

Hi

This article may be useful for .NET developers while deploy the project

There may be the need of Database Creation in Runtime according to developer's Project.

The following function will helps the above



private void ExecuteSQLScript(string Filename, System.Data.SqlClient.SqlConnection conn)
{

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
System.IO.StreamReader Reader = null;

try {
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;

Reader = new System.IO.StreamReader(Filename);
string s = Reader.ReadToEnd();
s = Strings.Replace(s, "GO", "~");
//Replace GO with a "~". Split only works with char
char[] delimiter = "~".ToCharArray();
string[] SQL = s.Split(delimiter);
//Now split the different SQL statements into an array

//Loop through array, executing each statement separately
for (int I = 0; I <= Information.UBound(SQL); I++) {
cmd.CommandText = SQL[I];
cmd.ExecuteNonQuery();
}

Reader.Close();
Reader = null;
cmd = null;

} catch (Exception ex) {
MessageBox.Show(ex.Message);
}

}




Finally you get the Database created.

You must get the database script file from SQL SERVER while developing project itself and put it in Deployment Folder(i.e needed file to run software)



Thanks and regards
Thirumalaisami S


Comments

Author: Zahid Sikder17 Sep 2010 Member Level: Bronze   Points : 1

I think this article would be helpful for .NET developers while deploy the project.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: