C#连接firebird

来源:互联网 发布:永川茶竹网软件 编辑:程序博客网 时间:2024/06/04 23:31

Firebird .NET Data Provider是一个用来操作Firebird数据库的数据访问组件,目前的版本是1.7。该组件提供了访问和操作Firebird数据库的各种函数,其使用非常简单,使用符合ADO.NET的规范。因此在使用上我们不会有太大的陌生感。

在安装Firebird .NET Data Provider后,其提供了一个SDK文档,通过它,我们可以快速的了解并使用该组件。

在编写一个数据库应用程序的时候,第一步肯定是建立一个和要操作数据库的连接,一般我们都是使用连接字符串,而通常情况下,这个连接字符串是比较复杂的,不是十分容易记忆。Firebird .NET Data Provider提供了一个名为FbConnectionStringBuilder的类,通过它我们可以很方便的构造一个连接字符串。

FirebirdSql.Data.Firebird.FbConnectionStringBuilder cs=new FirebirdSql.Data.Firebird.FbConnectionStringBuilder();
cs.DataSource="localhost";
cs.Database=@"d:\firebird\firsttest.gdb";
cs.UserID="sysdba";
cs.Password="masterkey";
cs.Dialect=1;

FirebirdSql.Data.Firebird.FbConnection cn=new FirebirdSql.Data.Firebird.FbConnection();
cn.ConnectionString=cs.ToString();

其后操作数据库的方法就和使用ADO.NET操作SQL Server或是Access数据库没什么区别了(除了使用的类名称不同外)。

下面是填充数据集的代码
     cn.Open();
     string strSQL="select * from T_1";
     FirebirdSql.Data.Firebird.FbDataAdapter ad=new FirebirdSql.Data.Firebird.FbDataAdapter(strSQL,cn);
     System.Data.DataSet ds=new System.Data.DataSet();
     ad.Fill(ds);
     cn.Close();

下面是向已连接的数据库中插入记录
     cn.Open();
     string strSQL="insert into T_1 values("1,'2005-7-4','testvalue')";
     FirebirdSql.Data.Firebird.FbCommand cm=new FirebirdSql.Data.Firebird.FbCommand();
     cm.Connection=cn;
     cm.CommandType=System.Data.CommandType.Text;
     cm.CommandText=strSQL;
     cm.ExecuteNonQuery();
     cn.Close();