本文主要介紹用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運行程序