嵌入式firebird VS2015測試例項

2021-12-29 21:53:57 字數 4551 閱讀 4043

示例errchecktest

為容易出錯的**段加上trycatch (exception ex) 防止崩潰。

建立vs2015 c#專案:errchecktest

insertdatatest模板

修改**

程式執行時,在按扭上胡亂點,哪個語句有問題,就用下面的格式:

try有問題的**

catch(exception ex)

messagebox.show(ex.message);

//或者輸出自己確定的出問題原因

完整**

using system;

using system.data;

using system.windows.forms;

using system.io;

using firebirdsql.data.firebirdclient;

namespace errchecktest

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();

privatevoid btncreatedb_click(object sender, eventargs e)

//建立資料庫

string path = application.startuppath + "\\data\\測試.fdb";

tryif (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());

trymyconnection.open();

messagebox.show("開啟連線成功!");

catch (exception ex)

messagebox.show(ex.message);

privatevoidbtncreatetable_click(object sender, eventargs e)

//建立空表

tryfbcommand createtable = myconnection.createcommand();

createtable.commandtext = "create table testtbl (id int,\"姓名\" varchar(20))";

createtable.executenonquery();

createtable.dispose();

messagebox.show("建立空表成功!");

catch (exception ex)

messagebox.show(ex.message);

privatevoidbtnbindd**iew_click(object sender, eventargs e)

//datagridview顯示資料

fbdataadapter dt = newfbdataadapter("select * from testtbl", myconnection);

dataset ds = newdataset();

trydt.fill(ds, "testtbl");

this.d**iewdb.datasource = ds;

this.d**iewdb.datamember = "testtbl";

messagebox.show("datagridview顯示資料!");

catch (exception ex)

messagebox.show(ex.message);

dt.dispose();

ds.dispose();

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("插入資料成功!");

catch (exception ex)

messagebox.show(ex.message);

privatevoidbtnreaddata_click(object sender, eventargs e)

//讀取資料

tryfbcommand 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)

//關閉連線

trymyconnection.close();

messagebox.show("連線已關閉!");

catch (exception ex)

messagebox.show(ex.message);

測試成功

但下面問題未解決:只要開啟一次資料庫,無論關不關閉連線,都會出現?誰能給出個原因啊!

嵌入式firebird VS2015例項十三

command 物件定義了將對資料來源執行的指定命令。l ado command 物件用於執行面向資料庫的一次簡單查詢。此查詢可執行諸如建立 新增 取回 刪除或更新記錄等動作。l 如果該查詢用於取回資料,此資料將以乙個 recordset 物件返回。這意味著被取回的資料能夠被 recordset 物...

嵌入式firebird VS2015例項十六

fbcommand類提供了乙個parameters集合屬性,用以為命令儲存所有的引數。其定義如下 public fbparameteradd string parametername,object value public fbparameteradd string parametername,fb...

初識嵌入式 嵌入式開發概述

1 什麼是嵌入式技術 1 嵌入式軟體與非嵌入式軟體的區別?答 嵌入式軟體是結合作業系統之上做的開發 非嵌入式軟體是做的裸機開發。裸機 沒有作業系統 2 嵌入式開發與微控制器開發的區別?答 區別 是否有作業系統。拓展 答 優點 解決了軟體的移植性 解決了開發人員的能力的劃分問題。提供了豐富的網路協議 ...