嵌入式firebird+VS2015实例十四
示例CreateCommandTest
CreateCommand方法
创建并返回与FbConnection关联的FbCommand对象。
CreateCommand属于FbConnection类的方法,它的返回值是FbCommand类型的,见定义:
namespace FirebirdSql.Data.FirebirdClient
{
[DefaultEvent("InfoMessage")]
publicsealedclassFbConnection : DbConnection, ICloneable
{
…
publicFbCommand CreateCommand();
…
}
}
使用CreateCommand方法与不使用没有本质上的不同,只不过这会使得选择上更加灵活多样。
代码示例
实例
创建项目:CreateCommandTest
FbCommandTest模板
完整代码
using System;
using System.Data;
using System.Windows.Forms;
using System.IO;
using FirebirdSql.Data.FirebirdClient;
namespace CreateCommandTest
{
publicpartialclassFrmMain : Form
{
publicFbConnection myConnection;
public FrmMain()
{
InitializeComponent();
myConnection = newFbConnection(GetConnectionString());
}
staticstringGetConnectionString()
{//构建连接字符串
FbConnectionStringBuilder cs = newFbConnectionStringBuilder();
cs.UserID = "SYSDBA";
cs.Password = "masterkey";
cs.Database = Application.StartupPath + "\\data\\测试.fdb";
cs.DataSource = "localhost";
cs.Charset = "UTF8";
cs.Port = 3050;
cs.Dialect = 3;
cs.Role = "";
cs.ConnectionLifeTime = 15;
/*返回一个连接池时,它的创建时间与当前时间相比,
* 和连接破坏如果时间(以秒计)超过指定的值连接。*/
cs.Pooling = true;//当真正从池中连接了,或者如果有必要,创建和添加到适当的池。
cs.MinPoolSize = 0;//池中所允许的最小连接数。
cs.MaxPoolSize = 50;//池中允许的最大连接数。
cs.PacketSize = 8192;
cs.ServerType = FbServerType.Embedded;
return cs.ToString();
}
privatevoid btnCreateDB_Click(object sender, EventArgs e)
{//创建数据库
string path = Application.StartupPath + "\\data\\测试.fdb";
try
{
if (File.Exists(path))
{
File.Delete(path);
FbConnection.CreateDatabase(GetConnectionString());
MessageBox.Show("创建数据库成功!");
}
else
{
FbConnection.CreateDatabase(GetConnectionString());
MessageBox.Show("创建数据库成功!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbtnTestConn_Click(object sender, EventArgs e)
{//测试连接
try
{
myConnection.Open();
MessageBox.Show("测试连接成功!");
myConnection.Close();//关闭连接
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbtnCreateTable_Click(object sender, EventArgs e)
{//创建表
//创建空表SQL
myConnection.Open();//打开连接
FbCommand createTable = myConnection.CreateCommand();
//CreateCommand,创建并返回 DbCommand 与当前连接关联的对象。
createTable.CommandText = "create table TestTBL (id int,\"姓名\" varchar(20))";
//插入数据
FbCommand insertData = myConnection.CreateCommand();
try
{
createTable.ExecuteNonQuery();//创建空表执行SQL
//插入数据一
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();
BinddgView(); //显示数据
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
createTable.Dispose();
insertData.Dispose();
myConnection.Close();//关闭连接
}
privatevoid BinddgView()
{//DataGridView显示数据
FbDataAdapter dt = newFbDataAdapter("select * from TestTBL", myConnection);
DataSet ds = newDataSet();
try
{
dt.Fill(ds, "TestTBL");
this.dgViewDB.DataSource = ds;
this.dgViewDB.DataMember = "TestTBL";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
dt.Dispose();
ds.Dispose();
}
privatevoidbtnUpdateTBL_Click(object sender, EventArgs e)
{//更新数据
myConnection.Open();//打开连接
string updateQuery = "Update TestTBL set \"姓名\"='小李'" + "Where ID='1'";
//FbCommand UpdateCMD = newFbCommand(updateQuery, myConnection);
FbCommand UpdateCMD = myConnection.CreateCommand();//CreateCommand方法
UpdateCMD.CommandText =updateQuery;//CreateCommand方法
try
{
int RecordsAffected = UpdateCMD.ExecuteNonQuery();//返回值为该命令所影响的行数
MessageBox.Show("影响行数:" + RecordsAffected);
BinddgView(); //显示数据
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
UpdateCMD.Dispose();
myConnection.Close();//关闭连接
}
}
}
执行