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运行程序
[ 相关贴图 ] 
[ 相关贴图 ] 
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


