对.Net数据进行适配[旧代码]
添加引用: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>
/// 数据类
/// </summary>
public class DataService
{
/// <summary>
/// 创建数据库连接
/// </summary>
/// <returns>数据库连接实例</returns>
/// <remarks>从Config文件中读取数据库连接字符串</remarks>
public static OleDbConnection CreateConnection()
{
//读取数据库连接字符串
string strConn = ConfigurationManager.AppSettings["dbconn"];
OleDbConnection conn = new OleDbConnection();
//指定数据库连接字符串
conn.ConnectionString = strConn;
//返回数据库连接实例
return conn;
}
/// <summary>
/// 创建适配器
/// </summary>
/// <param name="strSelect">SELECT语句</param>
/// <returns>适配器</returns>
public static OleDbDataAdapter CreateAdapter(string strSelect)
{
//创建数据库连接
OleDbConnection conn = CreateConnection();
//返回适配器
return new OleDbDataAdapter(strSelect, conn);
}
}
/// <summary>
/// 中间类
/// </summary>
public class UI
{
/// <summary>
/// 自身静态实例
/// </summary>
public static readonly UI NORMAL = new UI();
/// <summary>
/// 创建DataGridView实例
/// </summary>
/// <returns>DataGridView实例</returns>
public virtual DataGridView CreateGridView()
{
DataGridView gridview = new DataGridView();
gridview.Dock = DockStyle.Fill;
//显示标题行
gridview.ColumnHeadersVisible = true;
return gridview;
}
}
/// <summary>
/// 表现类
/// </summary>
public class Show : Form
{
/// <summary>
/// 构造函数
/// </summary>
public Show()
{
DataSet ds = new DataSet();
//SELECT语句
string strSelect = "SELECT * FROM ROCKET WHERE 1=1";
OleDbDataAdapter apdapter = DataService.CreateAdapter(strSelect);
apdapter.Fill(ds, "rocket");
apdapter.Dispose();
//创建DataGridView
DataGridView gridview = UI.NORMAL.CreateGridView();
//设置数据源
gridview.DataSource = ds;
//指定数据表名
gridview.DataMember = "rocket";
//添加gridview控件
Controls.Add(gridview);
Text = "显示数据";
}
}
}
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>
/// 数据类
/// </summary>
public class DataService
{
/// <summary>
/// 创建数据库连接
/// </summary>
/// <returns>数据库连接实例</returns>
/// <remarks>从Config文件中读取数据库连接字符串</remarks>
public static OleDbConnection CreateConnection()
{
//读取数据库连接字符串
string strConn = ConfigurationManager.AppSettings["dbconn"];
OleDbConnection conn = new OleDbConnection();
//指定数据库连接字符串
conn.ConnectionString = strConn;
//返回数据库连接实例
return conn;
}
/// <summary>
/// 创建适配器
/// </summary>
/// <param name="strSelect">SELECT语句</param>
/// <returns>适配器</returns>
public static OleDbDataAdapter CreateAdapter(string strSelect)
{
//创建数据库连接
OleDbConnection conn = CreateConnection();
//返回适配器
return new OleDbDataAdapter(strSelect, conn);
}
}
/// <summary>
/// 中间类
/// </summary>
public class UI
{
/// <summary>
/// 自身静态实例
/// </summary>
public static readonly UI NORMAL = new UI();
/// <summary>
/// 创建DataGridView实例
/// </summary>
/// <returns>DataGridView实例</returns>
public virtual DataGridView CreateGridView()
{
DataGridView gridview = new DataGridView();
gridview.Dock = DockStyle.Fill;
//显示标题行
gridview.ColumnHeadersVisible = true;
return gridview;
}
}
/// <summary>
/// 表现类
/// </summary>
public class Show : Form
{
/// <summary>
/// 构造函数
/// </summary>
public Show()
{
DataSet ds = new DataSet();
//SELECT语句
string strSelect = "SELECT * FROM ROCKET WHERE 1=1";
OleDbDataAdapter apdapter = DataService.CreateAdapter(strSelect);
apdapter.Fill(ds, "rocket");
apdapter.Dispose();
//创建DataGridView
DataGridView gridview = UI.NORMAL.CreateGridView();
//设置数据源
gridview.DataSource = ds;
//指定数据表名
gridview.DataMember = "rocket";
//添加gridview控件
Controls.Add(gridview);
Text = "显示数据";
}
}
}