示例RefreshTest
数据表变动应该即时显示给用户,而不是次次点击刷新。
修改代码
privatevoidbtnBinddgView_Click(object sender, EventArgs e)
{
//DataGridView显示数据
…
}
为:
privatevoid BinddgView ()
{
//DataGridView显示数据
…
}
并在更新数据后调用BinddgView ()
实例
创建项目:RefreshTest
ClearPoolTest模板
完整代码
using System;
using System.Data;
using System.Windows.Forms;
using System.IO;
using FirebirdSql.Data.FirebirdClient;
namespace RefreshTest
{
publicpartialclassFrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
publicFbConnection myConnection;
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";
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);
}
}
privatevoidbtnOpenDB_Click(object sender, EventArgs e)
{
//打开数据库
myConnection = newFbConnection(GetConnectionString());
try
{
myConnection.Open();
MessageBox.Show("打开连接成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbtnCreateTable_Click(object sender, EventArgs e)
{
//创建空表
try
{
FbCommand createTable = myConnection.CreateCommand();
createTable.CommandText = "create table TestTBL (id int,\"姓名\" varchar(20))";
createTable.ExecuteNonQuery();
createTable.Dispose();
MessageBox.Show("创建空表成功!");
BinddgView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
privatevoid BinddgView()
{
//DataGridView显示数据
try
{
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();
MessageBox.Show("DataGridView显示数据!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbtnInsertData_Click(object sender, EventArgs e)
{
//插入数据
try
{
//插入数据一
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();
MessageBox.Show("插入数据成功!");
BinddgView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbtnReadData_Click(object sender, EventArgs e)
{
//读取数据
try
{
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();
MessageBox.Show("读取数据成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbtnConnectionClose_Click(object sender, EventArgs e)
{
//关闭连接
try
{
myConnection.Close();
//FbConnection.ClearPool(myConnection);
//FbConnection.ClearAllPools();
MessageBox.Show("连接已关闭!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}