數據綁定根據不同組件可以分為二種
一. 本文程序設計和運行的軟件環境
(
(
(
二. 程序中使用的數據庫的數據字典
(
字段名稱 字段類型 字段意思 id 數字 序號 xm 文本 姓名 xb 文本 性別 nl 文本 年齡 zip 文本 郵政編碼
(
三. 數據綁定一般步驟
(一)
(
//創建一個 OleDbConnection
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
myConn
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
(
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
(二)
對於簡單型的數據綁定
三. 數據綁定一般步驟
(一)
(
//創建一個 OleDbConnection
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
myConn
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
(
// 設定數據連接字符串
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
(二)
對於簡單型的數據綁定
四.簡單型組件的數據綁定
(
通過下列語句就可以把數據集(即為
組件的
textBox
注釋
由此可以得到綁定TextBox組件的源程序代碼(TextBox
public class Form
{
private TextBox textBox
private Button button
private System
private System
public Form
{
file://打開數據鏈接
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components
}
}
base
}
private void GetConnect ( )
{
file://創建一個 OleDbConnection
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
myConn
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
}
private void button
{
textBox
}
static void Main ( )
{
Application
}
}
圖
public class Form
{
private TextBox textBox
private Button button
private System
private System
public Form
{
file://打開數據鏈接
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components
}
}
base
}
private void GetConnect ( )
{
// 設定數據連接字符串
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
}
private void button
{
textBox
}
static void Main ( )
{
Application
}
}
在上面的介紹中
(
在得到數據集後
ComboBox
ComboBox
ComboBox
注釋
public class Form
{
private ComboBox ComboBox
private Button button
private System
private System
public Form
{
file://打開數據鏈接
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components
}
}
base
}
private void GetConnect ( )
{
file://創建一個 OleDbConnection
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
myConn
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
}
private void button
{
ComboBox
ComboBox
ComboBox
}
static void Main ( )
{
Application
}
}
圖
得到了ComboBox組件對本地數據庫的數據綁定程序
public class Form
{
private ComboBox ComboBox
private Button button
private System
private System
public Form
{
file://打開數據鏈接
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components
}
}
base
}
private void GetConnect ( )
{
// 設定數據連接字符串
稱為server
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
}
private void button
{
ComboBox
ComboBox
ComboBox
}
static void Main ( )
{
Application
}
}
ListBox組件的數據綁定和ComboBox組件的數據綁定的方法大致相同
ListBox
public class Form
{
private ListBox ListBox
private Button button
private System
private System
public Form
{
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components
}
}
base
}
private void GetConnect ( )
{
file://創建一個 OleDbConnection
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
myConn
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
}
private void button
{
ListBox
ListBox
ListBox
}
static void Main ( )
{
Application
}
}
圖
以下代碼是ListBox組件對Sql Server
{
private ListBox ListBox
private Button button
private System
private System
public Form
{
file://打開數據鏈接
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components
}
}
base
}
private void GetConnect ( )
{
// 設定數據連接字符串
string strCon =
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn
string strCom =
file://創建一個 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom
file://把Dataset綁定person數據表
myCommand
file://關閉此OleDbConnection
myConn
}
private void button
{
ListBox
ListBox
ListBox
}
static void Main ( )
{
Application
}
}
六. 總結
本文介紹的實現數據綁定組件的都是在程序設計中經常用到的WinForm組件
當然在
From:http://tw.wingwit.com/Article/program/net/201311/11707.html