Add reference: system. configuration. dll, system. Windows. Forms. dll
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. configuration;
Using system. Data;
Using system. Data. oledb;
Using system. Windows. forms;
Namespace patternsstudy3
{
Class Program
{
Static void main (string [] ARGs)
{
Application. Run (new show ());
}
}
/// <Summary>
/// Data class
/// </Summary>
Public class dataservice
{
/// <Summary>
/// Create a database connection
/// </Summary>
/// <Returns> database connection instance </returns>
/// <Remarks> Read the database connection string from the config file </remarks>
Public static oledbconnection createconnection ()
{
// Read the database connection string
String strconn = configurationmanager. receivettings ["dbconn"];
Oledbconnection conn = new oledbconnection ();
// Specify the database connection string
Conn. connectionstring = strconn;
// Return to the database connection instance
Return conn;
}
/// <Summary>
/// Create an adapter
/// </Summary>
/// <Param name = "strselect"> SELECT statement </param>
/// <Returns> adapter </returns>
Public static oledbdataadapter createadapter (string strselect)
{
// Create a database connection
Oledbconnection conn = createconnection ();
// Return the adapter
Return new oledbdataadapter (strselect, Conn );
}
}
/// <Summary>
/// Intermediate class
/// </Summary>
Public class UI
{
/// <Summary>
/// Static instance
/// </Summary>
Public static readonly UI normal = new UI ();
/// <Summary>
/// Create a dview instance
/// </Summary>
/// <Returns> datagridview instance </returns>
Public Virtual datagridview creategridview ()
{
Datagridview gridview = new datagridview ();
Gridview. Dock = dockstyle. Fill;
// Display the title line
Gridview. columnheadersvisible = true;
Return gridview;
}
}
/// <Summary>
/// Performance class
/// </Summary>
Public class show: Form
{
/// <Summary>
/// Constructor
/// </Summary>
Public Show ()
{
Dataset DS = new dataset ();
// SELECT statement
String strselect = "select * from rocket where 1 = 1 ";
Oledbdataadapter apdapter = dataservice. createadapter (strselect );
Apdapter. Fill (DS, "Rocket ");
Apdapter. Dispose ();
// Create a dview
Datagridview gridview = UI. Normal. creategridview ();
// Set the data source
Gridview. datasource = Ds;
// Specify the data table name
Gridview. datamember = "Rocket ";
// Add the gridview Control
Controls. Add (gridview );
TEXT = "show data ";
}
}
}