Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:howto:create_database_using_firebird [2011/02/25 16:15] smayr [Creating Database Programmatically (C#)] |
swdev:howto:create_database_using_firebird [2011/02/25 16:17] (current) smayr [Run an embedded SQL Script against Firebird] |
||
---|---|---|---|
Line 355: | Line 355: | ||
void ButtonRunScriptClick(object sender, System.EventArgs e) | void ButtonRunScriptClick(object sender, System.EventArgs e) | ||
{ | { | ||
- | //specify the database name | + | |
- | string dbName = " | + | string dbName = " |
- | //instanciate | + | //instantiate |
- | FbConnectionStringBuilder csb = new FbConnectionStringBuilder(); | + | FbConnectionStringBuilder csb = new FbConnectionStringBuilder(); |
- | csb.Database = dbName; | + | csb.Database = dbName; |
- | csb.UserID = " | + | csb.UserID = " |
- | csb.Password = " | + | csb.Password = " |
- | csb.ServerType = 1; // | + | csb.ServerType = 1; // |
- | //instanciate | + | //instantiate |
- | FbConnection con = new FbConnection(csb.ToString()); | + | FbConnection con = new FbConnection(csb.ToString()); |
- | //check if the database exists | + | //check if the database exists |
- | if (System.IO.File.Exists(dbName) == false) | + | if (System.IO.File.Exists(dbName) == false) |
- | //create the database as it didn’t exist | + | //create the database as it didn’t exist |
- | FbConnection.CreateDatabase(csb.ToString()); | + | FbConnection.CreateDatabase(csb.ToString()); |
- | //run the script against the current connection | + | //run the script against the current connection |
- | RunScript(" | + | RunScript(" |
} | } | ||
void RunScript(string ScriptName, FbConnection connection) | void RunScript(string ScriptName, FbConnection connection) | ||
{ | { | ||
- | //get a reference to the executing assembly | + | |
- | System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); | + | System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); |
- | //instanciate | + | //instantiate |
- | System.IO.TextReader textReader = new System.IO.StreamReader(assembly.GetManifestResourceStream(ScriptName)); | + | System.IO.TextReader textReader = new System.IO.StreamReader(assembly.GetManifestResourceStream(ScriptName)); |
- | FbScript script = new FbScript(textReader); | + | FbScript script = new FbScript(textReader); |
- | //parse the script | + | //parse the script |
- | script.Parse(); | + | script.Parse(); |
- | //open the connection | + | //open the connection |
- | connection.Open(); | + | connection.Open(); |
- | FbBatchExecution fbe = new FbBatchExecution(connection); | + | FbBatchExecution fbe = new FbBatchExecution(connection); |
- | foreach (string cmd in script.Results) | + | foreach (string cmd in script.Results) |
- | { | + | { |
- | //add each sql statement to the batch | + | //add each sql statement to the batch |
- | fbe.SqlStatements.Add(cmd); | + | fbe.SqlStatements.Add(cmd); |
- | } | + | } |
- | //execute the batch | + | //execute the batch |
- | fbe.Execute(); | + | fbe.Execute(); |
- | //close the connection | + | //close the connection |
- | connection.Close(); | + | connection.Close(); |
} | } | ||
</ | </ | ||
Line 407: | Line 407: | ||
# Create a text file named ' | # Create a text file named ' | ||
CREATE TABLE CLIENTS ( | CREATE TABLE CLIENTS ( | ||
- | client_id integer not null, | + | |
- | firstname char(20), | + | firstname char(20), |
- | lastname char(20), | + | lastname char(20), |
- | PRIMARY KEY (client_id)); | + | PRIMARY KEY (client_id)); |
# Add the file you created in Step 4 to your project and set the build action to Embedded Resource. | # Add the file you created in Step 4 to your project and set the build action to Embedded Resource. | ||
# Add a button to your form named buttonRunScript and check that its Click Event is set to the ButtonRunScriptClick Event Handler. | # Add a button to your form named buttonRunScript and check that its Click Event is set to the ButtonRunScriptClick Event Handler. |