示例CreateTableTest
使用FbConnectionmyConnection定义连接,如果有多处用它,就不能声明成局部变量。
建立VS2015 C#项目:CreateTableTest
OpenDatabaseTest模板
修改代码
1、增加一个按钮:btnCreateTable,“创建空表”,双击,双黄线中间部分不动,其它代码移动过来:
2、但这里会出问题,因为myConnection是个局部变量:
3、要对myConnection重新定义:
publicFbConnection myConnection;
…
myConnection = newFbConnection(GetConnectionString());
完整代码
using System;
using System.Data;
using System.Windows.Forms;
using System.IO;
using FirebirdSql.Data.FirebirdClient;
namespace CreateTableTest
{
publicpartialclassFrmMain : Form
{
publicFbConnection myConnection;
privatevoidbtnOpenDB_Click(object sender, EventArgs e)
{
//打开数据库
myConnection = newFbConnection(GetConnectionString());
try
{
myConnection.Open();
MessageBox.Show("Open a connection");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public FrmMain()
{
InitializeComponent();
}
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();
}
privatevoidbtnCreateDB_Click(object sender, EventArgs e)
{
//创建数据库
string path = Application.StartupPath + "\\data\\测试.fdb";
if (File.Exists(path))
{
File.Delete(path);
FbConnection.CreateDatabase(GetConnectionString());
MessageBox.Show("文件存在,删除重建!");
}
else
{
FbConnection.CreateDatabase(GetConnectionString());
MessageBox.Show("文件不存在,直接创建!");
}
}
privatevoidbtnCreateTable_Click(object sender, EventArgs e)
{
//创建空表
FbCommand createTable = myConnection.CreateCommand();
createTable.CommandText = "create table TestTBL (id int,\"姓名\" 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();
//读取数据
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显示数据
FbDataAdapter dt = newFbDataAdapter("select * from TestTBL", myConnection);
DataSet ds = newDataSet();
dt.Fill(ds, "TestTBL");
this.dgViewDB.DataSource = ds;
this.dgViewDB.DataMember = "TestTBL";
dt.Dispose();
ds.Dispose();
//关闭连接
myConnection.Close();
}
}
}
测试成功