Construction Database Application with C # Builder

zhaozj2021-02-08  241

Author: Xu Changyou home page: http: //yousoft.hi.com.cn This paper describes using C # Builder to access data through ODBC. And export data to Excel, following C # Builder Enterprise Microsoft Access 2000 Microsoft Excel 2000 as an example. 1. Establish a database MYDB, build a table: contact contact ID name last name is address city province [related map] 2. Establish an ODBC (MYDB) 3. Write program Click on the menu File - New - C # Application, Enter the application name [ Related textures] [related map] If you haven't installed an ODBC component, you still need to install them. Click on Menu Component - Installed .NET Components, make sure ODBC components are selected in the Installed .Net Components window. After confirming, see if there is several components of the ODBC on the Tool Palette. Icon [Related texture] [Related map] plus a OdbcConnection the OdbcCommand and a [map associated] selected odbcConnection1, in the input ConnectionString: DSN = mydb; Uid = admin; Pwd =; selected odbcCommand1, Connection select odbcConnection1, CommandText type: select * From contact WinForm window plus two Button and Listbox, where Listbox's Dock is set to Bottom Double-click button, enter code: 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 ();} For some of the .NET's ODBC, you can view the help. It is written in detail above. [Related Map] Complete data export via COM component Export Excel: In order to use Excel in C #, we must first do a little ready to work, find Tlbimp and Excel9.olb in your computer, copy them into a folder, in TLBIMP Excel9.olb is executed in the DOS window, and three files are generated: Excel.dll, Office.dll and Vbide.dll.

By menu project-> add reference, select COM IMPORTS in the pop-up dialog box, click the Browser button, select the three DLL files generated in front, the OK export code is as follows: // Create an Excel file INT i; Excel.Application myexcel = New Excel.application () myexcel.application.workbooks.add (true) // Let the Excel file can be seen by myexcel.visible = true; // First behavior report name MyExcel.cells [1, 4] = "Contact"; MyExcel. Cells [2, 1] = "Contact ID"; MyExcel.cells [2, 2] = "Name"; myexcel.cells [2, 3] = "last name"; myexcel.cells [2, 4] = "address "; Myexcel.cells [2,5] =" city "; myexcel.cells [2,6] =" province "; // progressive write data, listbox1.items.clear (); odbcconnection1.open (); Odbcdataareader 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 ();} complete program code is as follows: 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; use system.reflection; namespace dbapp {///

/// summary description for winform. /// public class winform: system.windows.forms.form {/// < Summary> /// Required Designer Variable. /// 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 // InitializationComponent (); // // Todo: Add Any Constructor Code After InitializationComponent Call / /} ///

/// clean Up Any Resources Being used. /// protected {if (disponents! = null) {components.dispose ();}}}}}}}}}} #region;} #Region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor /// private void InitializeComponent () {this. .odbcconnection1 = new system.data.odbc.odbcconnection (); this.listbox1 = new system.win Dows.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 = " queries "; this.button1.Click = new System.EventHandler (this.button1_Click); // // odbcCommand1 // this.odbcCommand1.CommandText =" select * from contact "; 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 = "Export"; 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.ResumeLayo UT (false);} #endregion ///

/// the main entry point for the application. /// [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) "

转载请注明原文地址:https://www.9cbs.com/read-2702.html

New Post(0)