在.net中调用datawindow操作数据库

来源:互联网 发布:淘宝账号异常怎么回事 编辑:程序博客网 时间:2024/04/30 07:42
sybase在2004/1/19日推出了pb 10 beta1版本,其中包含datawindow.net 1.0,这真是一个另人兴奋的消息! 在pb市场日益萎缩的今天,在.net大行其道的今天sybase公司终于推出了datawindow.net,它支持datawindow绝大部份原有事件和属性,pb技术终于可以重新又派上用场了!!

下过来安装程序,便急不可待的在vs.net2003下面做了一个简单的例子,发现真的很好用!源代码、界面如下:



using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;

namespace cjgl.report
{
/// <summary>
/// datawindowtest 的摘要说明。
/// </summary>
public class datawindowtest : system.windows.forms.form
{
private sybase.datawindow.datawindowcontrol dw;
private sybase.datawindow.transaction trans;
private system.windows.forms.button btnretrieve;
private system.windows.forms.button btndelete;
private system.windows.forms.button btninsert;
private system.windows.forms.button btnsave;
private system.componentmodel.icontainer components;

public datawindowtest()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();

//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//


}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}

#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.components = new system.componentmodel.container();
this.btnretrieve = new system.windows.forms.button();
this.btndelete = new system.windows.forms.button();
this.btninsert = new system.windows.forms.button();
this.dw = new sybase.datawindow.datawindowcontrol();
this.trans = new sybase.datawindow.transaction(this.components);
this.btnsave = new system.windows.forms.button();
this.suspendlayout();
//
// btnretrieve
//
this.btnretrieve.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
this.btnretrieve.location = new system.drawing.point(360, 297);
this.btnretrieve.name = "btnretrieve";
this.btnretrieve.tabindex = 1;
this.btnretrieve.text = "retrieve";
this.btnretrieve.click += new system.eventhandler(this.btnretrieve_click);
//
// btndelete
//
this.btndelete.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
this.btndelete.location = new system.drawing.point(260, 296);
this.btndelete.name = "btndelete";
this.btndelete.tabindex = 2;
this.btndelete.text = "delete";
this.btndelete.click += new system.eventhandler(this.btndelete_click);
//
// btninsert
//
this.btninsert.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
this.btninsert.location = new system.drawing.point(152, 295);
this.btninsert.name = "btninsert";
this.btninsert.tabindex = 3;
this.btninsert.text = "insert";
this.btninsert.click += new system.eventhandler(this.btninsert_click);
//
// dw
//
this.dw.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom)
| system.windows.forms.anchorstyles.left)
| system.windows.forms.anchorstyles.right)));
this.dw.datawindowobject = "dw_customer";
this.dw.librarylist = "d://program files//sybase10//datawindow builder 1.0//dwb100.pbl";
this.dw.location = new system.drawing.point(5, 5);
this.dw.name = "dw";
this.dw.scrollbars = system.windows.forms.scrollbars.both;
this.dw.size = new system.drawing.size(432, 280);
this.dw.tabindex = 4;
this.dw.text = "datawindowcontrol1";
this.dw.typeofdatawindow = sybase.datawindow.datawindowtype.grid;
//
// trans
//
this.trans.database = "asademo9.dba";
this.trans.dbms = sybase.datawindow.dbmstype.oledb;
this.trans.dbparameter = "provider=/asaprov.90/";
this.trans.password = "sql";
this.trans.servername = "asademo9";
this.trans.userid = "dba";
//
// btnsave
//
this.btnsave.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
this.btnsave.location = new system.drawing.point(46, 296);
this.btnsave.name = "btnsave";
this.btnsave.tabindex = 5;
this.btnsave.text = "&save";
this.btnsave.click += new system.eventhandler(this.btnsave_click);
//
// datawindowtest
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(444, 333);
this.controls.add(this.btnsave);
this.controls.add(this.dw);
this.controls.add(this.btninsert);
this.controls.add(this.btndelete);
this.controls.add(this.btnretrieve);
this.name = "datawindowtest";
this.text = "利用datawindow.net设计";
this.resumelayout(false);

}
#endregion

private void btnretrieve_click(object sender, system.eventargs e)
{
try
{
if ( !trans.isconnected)
{
trans.connect();
}
dw.settransobject(trans);
dw.retrieve();

}
catch( sybase.datawindow.dberrorexception ee)
{
messagebox.show("数据库连接出错!"+ee.sqlerrortext);
return;
}
}

private void btninsert_click(object sender, system.eventargs e)
{
//插入一行
int insertrow = dw.insertrow(0);
dw.scroll(insertrow);
//赋初值
dw.setitemdouble(insertrow,"id",9999);
dw.setitemstring(insertrow,"fname","huang");
dw.setitemstring(insertrow,"lname","yong");
dw.setitemstring(insertrow,"address","changsha");
dw.setitemstring(insertrow,"city","changsha");
}

private void btndelete_click(object sender, system.eventargs e)
{
dw.deleterow(dw.currentrow);
}

private void btnsave_click(object sender, system.eventargs e)
{
try
{
dw.update();
trans.commit();
}
catch (sybase.datawindow.dberrorexception ee)
{
messagebox.show("更新不成功!原因:"+ee.sqlerrortext);
trans.rollback();
}
catch (sybase.datawindow.datawindownotcreatedexception ee)
{
messagebox.show("数据窗口还没有创建!");
trans.rollback();
}
catch(sybase.datawindow.methodfailureexception ee)
{
messagebox.show("更新不成功!原因:"+ee.message.tostring());
trans.rollback();
}
}

}
}
通过pb,或datawindow builder创建pbl库和datawindow对象后,就像在pb中操作一样,指定pbl库位置和datawindow控件相关连datawindow对象,就可对datawindow进行相关的操作如dw.settransobject(trans);dw.retrieve();怎么样?对原pb
程序员来说很熟悉吧!