电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> 数据库
用C#Builder建数据库应用程序-.NET教程,C#语言
作者:网友供稿 点击:19
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
本文主要介绍用c# builder通过odbc访问数据。并将数据导出到excel,下面以c# builder enterprise+microsoft access 2000+microsoft excel 2000为例。

  1.建立数据库mydb,内建表:联系人

     联系人id
     名字
     姓氏
     地址
     城市
     省份

   [ 相关贴图 ]



   2.建立odbc(mydb)

  3.编写程序

   点击菜单 file - new - c# application,输入应用程序名称

[ 相关贴图 ]

[ 相关贴图 ]

  如果还没有安装odbc组件,你还需要装上它们。点击菜单component - installed .net components,在installed .net components窗口中确定odbc组件已经选上。确定后,看tool palette上是否有odbc几个组件在上面。图示

[ 相关贴图 ]

[ 相关贴图 ]

  加上一个odbcconnection和一个odbccommand

[ 相关贴图 ]

  选中odbcconnection1,在connectionstring输入:dsn=mydb;uid=admin;pwd=;
选中odbccommand1,connection选择odbcconnection1,commandtext输入:select * from 联系人
winform窗口加上两个button和listbox,其中listbox的dock设为bottom
双击按钮,输入代码:

listbox1.items.clear();
odbcconnection1.open();
odbcdatareader myreader=odbccommand1.executereader();
try{
while (myreader.read())
{
listbox1.items.add(myreader.getstring(0)+","+myreader.getstring(1)+" "+myreader.getstring(2));
}

}
finally{
myreader.close();
odbcconnection1.close();
}

   对于.net对odbc的一些使用方法,可以查看帮助。上面写得很详细。
[ 相关贴图 ]
>>-ǫ?㻷ʳ?격???󍼭<<
   通过com组件来完成数据导出excel:

   为了在c#中使用excel,我们要先做一点准备工作,在你的计算机中找到tlbimp和excel9.olb,将他们复制到一个文件夹中,在dos窗口中执行 tlbimp excel9.olb,这时会产生以下三个文件:excel.dll、office.dll和vbide.dll。

   通过菜单 project->add reference ,弹出的对话框中选择com imports,点击browser按钮,选中前面生成的三个dll文件,ok

  导出代码如下:


//创建一个excel文件
int i;
excel.application myexcel = new excel.application ( )

myexcel.application.workbooks.add ( true )

//让excel文件可见

myexcel.visible=true;

//第一行为报表名称

myexcel.cells[1,4]="联系人";
myexcel.cells[2,1]="联系人id";
myexcel.cells[2,2]="名字";
myexcel.cells[2,3]="姓氏";
myexcel.cells[2,4]="地址";
myexcel.cells[2,5]="城市";
myexcel.cells[2,6]="省份";

//逐行写入数据,

listbox1.items.clear();
odbcconnection1.open();
odbcdatareader myreader=odbccommand1.executereader();
try{
i=2;
while (myreader.read())
{
i=i+1;
myexcel.cells[i,1]=myreader.getstring(0);
myexcel.cells[i,2]=myreader.getstring(1);
myexcel.cells[i,3]=myreader.getstring(2);
myexcel.cells[i,4]=myreader.getstring(3);
myexcel.cells[i,5]=myreader.getstring(4);
myexcel.cells[i,6]=myreader.getstring(5);
}

}
finally{
myreader.close();
odbcconnection1.close();
}

程序完整的代码如下:

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.data.odbc;
using system.io;
using system.reflection;


namespace dbapp
{
/// <summary>
/// summary description for winform.
/// </summary>
public class winform : system.windows.forms.form
{
/// <summary>
/// required designer variable.
/// </summary>
private system.componentmodel.container components = null;
private system.data.odbc.odbcconnection odbcconnection1;
private system.data.odbc.odbccommand odbccommand1;
private system.windows.forms.listbox listbox1;
private system.windows.forms.button button1;
private system.windows.forms.button button2;

public winform()
{
//
// required for windows form designer support
//
initializecomponent();

//
// todo: add any constructor code after initializecomponent call
//
}

/// <summary>
/// clean up any resources being used.
/// </summary>
protected override void dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.dispose();
}
}
base.dispose(disposing);
}

#region windows form designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.odbcconnection1 = new system.data.odbc.odbcconnection();
this.listbox1 = new system.windows.forms.listbox();
this.button1 = new system.windows.forms.button();
this.odbccommand1 = new system.data.odbc.odbccommand();
this.button2 = new system.windows.forms.button();
this.suspendlayout();
//
// odbcconnection1
//
this.odbcconnection1.connectionstring = "dsn=mydb;uid=admin;pwd=;";
//
// listbox1
//
this.listbox1.dock = system.windows.forms.dockstyle.bottom;
this.listbox1.itemheight = 12;
this.listbox1.location = new system.drawing.point(0, 53);
this.listbox1.name = "listbox1";
this.listbox1.size = new system.drawing.size(368, 184);
this.listbox1.tabindex = 0;
//
// button1
//
this.button1.location = new system.drawing.point(16, 16);
this.button1.name = "button1";
this.button1.tabindex = 1;
this.button1.text = "查询";
this.button1.click += new system.eventhandler(this.button1_click);
//
// odbccommand1
//
this.odbccommand1.commandtext = "select * from 联系人 ";
this.odbccommand1.connection = this.odbcconnection1;
//
// button2
//
this.button2.location = new system.drawing.point(245, 14);
this.button2.name = "button2";
this.button2.tabindex = 2;
this.button2.text = "导出";
this.button2.click += new system.eventhandler(this.button2_click);
//
// winform
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(368, 237);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.controls.add(this.listbox1);
this.name = "winform";
this.text = "winform";
this.load += new system.eventhandler(this.winform_load);
this.resumelayout(false);
}
#endregion

/// <summary>
/// the main entry point for the application.
/// </summary>
[stathread]
static void main()
{
application.run(new winform());
}

private void winform_load(object sender, system.eventargs e)
{

}

private void button1_click(object sender, system.eventargs e)
{
listbox1.items.clear();
odbcconnection1.open();
odbcdatareader myreader=odbccommand1.executereader();
try{
while (myreader.read())
{
listbox1.items.add(myreader.getstring(0)+","+myreader.getstring(1)+" "+myreader.getstring(2));
}

}
finally{
myreader.close();
odbcconnection1.close();
}

}

private void button2_click(object sender, system.eventargs e)
{
//创建一个excel文件
int i;
excel.application myexcel = new excel.application ( )

myexcel.application.workbooks.add ( true )

//让excel文件可见

myexcel.visible=true;

//第一行为报表名称

myexcel.cells[1,4]="联系人";
myexcel.cells[2,1]="联系人id";
myexcel.cells[2,2]="名字";
myexcel.cells[2,3]="姓氏";
myexcel.cells[2,4]="地址";
myexcel.cells[2,5]="城市";
myexcel.cells[2,6]="省份";

//逐行写入数据,

listbox1.items.clear();
odbcconnection1.open();
odbcdatareader myreader=odbccommand1.executereader();
try{
i=2;
while (myreader.read())
{
i=i+1;
myexcel.cells[i,1]=myreader.getstring(0);
myexcel.cells[i,2]=myreader.getstring(1);
myexcel.cells[i,3]=myreader.getstring(2);
myexcel.cells[i,4]=myreader.getstring(3);
myexcel.cells[i,5]=myreader.getstring(4);
myexcel.cells[i,6]=myreader.getstring(5);
}

}
finally{
myreader.close();
odbcconnection1.close();
}

}
}
}

  4.运行程序

   按f9运行程序

[ 相关贴图 ]

[ 相关贴图 ]

add by : huobazi (2005-5-15:04:28)  

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·数据库开发个人总结(ADO.NET小结)-.NET教程,数据库应用
·怎么由DataSet将数据导入Excel?-.NET教程,数据库应用
·动态创建SQL Server数据库、表、存储过程-ASP教程,数据库相关
·Win32环境下动态链接库(DLL)编程原理-.NET教程,数据库应用
·封装的ADO.NET对数据库操作经典类-.NET教程,数据库应用
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·DataGrid使用心得(附大量代码)-ASP教程,数据库相关
·用代码创建DataGrid的多链接及checkbox事件响应-.NET教程,数据库应用
·ADO.NET 的最佳实践技巧-.NET教程,数据库应用
·转载: 用纯ASP代码实现图片上传并存入数据库中

最新文章
·根据数据表中数据,生成Powerpoint幻灯片-ASP教程,数据库相关
·DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon-ASP教程,数据库相关
·使用用VB处理MYSQL数据库中二进制数据问题-.NET教程,VB.Net语言
·关于DataGridView中如何接收处于编辑状态下的当前信息-ASP教程,数据库相关
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·.net下访问Access数据库需要注意的问题-.NET教程,Asp.Net开发
·ActiveMQ4.1+Spring2.0的POJO JMS方案(上)-.NET教程,数据库应用
·ASP.NET 2.0中直接将Access数据库导入到Excel文件中-.NET教程,Asp.Net开发
·NET(C#)连接各类数据库-集锦-.NET教程,C#语言
·ASP.NET2.0连接SQL Server数据库详解-.NET教程,Asp.Net开发


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号