Although having some C# code that can create an Embedded Firebird database, table, stored procedure, and insert a record is good to show what can work, it's not very reusable. I started making something of a support library.
The goal of the library was to be able to:
- Create databases
- Create tables with Autoincrement "id" field as primary key
- Create stored procedures related to those tables to INSERT records and return the "id" field
- Be somewhat generic in use so that it could be expanded to other type of databases
The code for the library got a bit long to just include in the post, so I've made it so that it can be downloaded as a file associated with this post.
Here's a console program that uses the library to create a database, a table, and the associated elements to support AutoIncrement, Primary Key, and return of auto created "id" upon insert.
/*
* Created by SharpDevelop.
* User: Brian Lakstins
* Date: 9/12/2005
* Time: 9:21 PM
*/
using System;
using System.Data;
using System.IO;
using BALConsultingNet.Data;
namespace DDLTest
{
class MainClass
{
public static void Main(string[] args)
{
//Make sure this thing is turned on.
Console.WriteLine("Hello World!");
//Define the name of the database
string lsDBName = "Test";
//Define the directory where the database file will go
string lsDataDir = Directory.GetCurrentDirectory();
//Create the database, returning the file name created
string lsDataFile = DDL.CreateDatabase(lsDBName,lsDataDir);
//Define a table for the database using the DataTable object
DataTable loTable = new DataTable();
loTable.TableName = "Person";
//Define a column in the table (name it "id" and have it be an Integer)
DataColumn loColumn = new DataColumn("id",Type.GetType("System.Int32"));
//Don't allow Nulls in this column
loColumn.AllowDBNull = false;
//Make this an AutoIncrement column
loColumn.AutoIncrement = true;
//Add it to the table definition
loTable.Columns.Add(loColumn);
//Add a constraint to make this column the primary key
UniqueConstraint loPrimaryKey = new UniqueConstraint("PK",loColumn);
//Add the constraint
loTable.Constraints.Add(loPrimaryKey);
//Add some text columns
loTable.Columns.Add("namefirst",Type.GetType("System.String"));
loTable.Columns.Add("namemiddle",Type.GetType("System.String"));
loTable.Columns.Add("namelast",Type.GetType("System.String"));
//Create the table
// associated generator for the AutoIncrement field
// associated constraint for the Primary key
DDL.CreateTable(lsDataFile,loTable);
//Create a stored procedure to use to insert data into the table (and return
// the "id" of the record created
string lsStoredProcedureInsertName = "SP_" + loTable.TableName + "INSERT";
DDL.CreateStoredProcedureInsert(lsDataFile,loTable,lsStoredProcedureInsertName);
}
}
}
BALConsultingNet.Data.cs.txt (12.11 KB)