嵌入式firebird+VS2015实例一
示例EmbedTest
实现在程序中嵌入firebird的基本功能
建立VS2015 C#项目
1、使用VS2015 在H:\根目录下建立C# windows窗体应用程序:EmbedTest
2、找到Form1.cs,重命名FrmMain.cs,则所有相关名称变为:FrmMain
3、修改窗体显示名称为:EmbedTest,在窗体中加入一个按钮,一个DataGridView控件,分别命名为:btnCreateDB(“创建数据库”),dgViewDB。
结果,查看黄色目录下文件:
Embedded Firebird
1、解压Firebird-2.5.8.27089-0_Win32_embed
解压FirebirdSql.Data.Firebird.Client-5.12.1.0-NET45
把解压出来的文件放到一起,删除不用的文件,结果如下:
黄色标明的就是firebird.msg。
2、全选复制到刚建立的项目bin\debug目录下:
3、添加引用FirebirdSql.Data.FirebirdClient.dll
引用成功后,它的引用属性中的路径:..\\EmbedTest\bin\Debug\(无论文件在哪里,VS2015会自动把这个文件自己移动到bin\debug目录下)。
双击按钮控件进入代码编辑:
引用程序集
using System.IO;
usingFirebirdSql.Data.FirebirdClient;
建立连接字符串
private void btnCreateDB_Click(object sender, EventArgs e)
{
// 连接字符串
string connectionString =
"User=SYSDBA;" +
"Password=masterkey;" +
"Database=test.fdb;" +
"DataSource=localhost;" +
"Port=3050;" +
"Dialect=3;" +
"Charset=NONE;" +
"Role=;" +
"Connection lifetime=15;" +
"Pooling=true;" +
"MinPoolSize=0;" +
"MaxPoolSize=50;" +
"Packet Size=8192;" +
"ServerType=1";
}
创建数据库
//创建数据库
string path = Application.StartupPath + "\\test.fdb";
if (File.Exists(path))
{
File.Delete(path);
FbConnection.CreateDatabase(connectionString);
MessageBox.Show("文件存在,删除重建!");
}
else
{
FbConnection.CreateDatabase(connectionString);
MessageBox.Show("文件不存在,直接创建!");
}
打开数据库
//打开数据库
FbConnection myConnection = new FbConnection(connectionString);
try
{
myConnection.Open();
MessageBox.Show("Open a connection");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
创建表
//创建表
FbCommand createTable = myConnection.CreateCommand();
createTable.CommandText = "create table TestTBL (id int,name varchar(20))";
createTable.ExecuteNonQuery();
createTable.Dispose();
插入数据
//插入数据
//插入数据一
FbCommand insertData = myConnection.CreateCommand();
insertData.CommandText = "insert into TestTBL values(@id, @name)";
insertData.Parameters.Clear();
insertData.Parameters.Add("@id", FbDbType.Integer).Value = 1;
insertData.Parameters.Add("@name", FbDbType.VarChar, 20).Value = "张三";
insertData.ExecuteNonQuery();
//插入数据二
insertData.CommandText = "insert into TestTBL values(@id, @胡作非为)";
insertData.Parameters.Clear();
insertData.Parameters.Add("@id", FbDbType.Integer).Value = 2;
insertData.Parameters.Add("@胡作非为", FbDbType.VarChar, 20).Value = "李四";
insertData.ExecuteNonQuery();
insertData.Dispose();
“@id, @name”前面的”@”表示”id,name”是一个局部变量,这是SQL的规定。局部变量不一定与字段名相同,如:“@胡作非为”
读取数据
//读取数据
FbCommand readData = myConnection.CreateCommand();
readData.CommandText = "select * from TestTBL";
FbDataReader Reader = readData.ExecuteReader();
while (Reader.Read())
{
string str_Temp = Reader.GetString(0);
MessageBox.Show(str_Temp);
str_Temp = Reader.GetString(1);
MessageBox.Show(str_Temp);
}
readData.Dispose();
显示数据(DataGridView)
//DataGridView显示数据
FbDataAdapter dt = new FbDataAdapter("select * from TestTBL", myConnection);
DataSet ds = new DataSet();
dt.Fill(ds, "TestTBL");
this.dgViewDB.DataSource = ds;
this.dgViewDB.DataMember = "TestTBL";
dt.Dispose();
ds.Dispose();
关闭连接
//关闭连接
myConnection.Close();
运行效果
生成模板,以便下次使用
VS2015-〉文件-〉导出模板: