国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

DataGrid實(shí)例(簡(jiǎn)單易懂,無(wú)復(fù)雜功能,適合初學(xué)者)

2019-11-18 17:18:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

使access數(shù)據(jù)庫(kù),適合初學(xué)者,修改連接、查詢語(yǔ)句后可直接運(yùn)行,代碼中有注明。

      填充DataSet的步驟
      1、使用數(shù)據(jù)庫(kù)連接字符串創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象
      2、用SQL查詢語(yǔ)句和數(shù)據(jù)庫(kù)連接對(duì)象創(chuàng)建數(shù)據(jù)庫(kù)適配器dataAdapter
      3、使用DataAdapter的Fill 方法填充DataSet


using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
//PRofessional C# 2nd的DATAGRID實(shí)例
/**////    <summary>
///    This class provides    an example of creating and using a data    grid.
///    </summary>
public class DisplayTabularData : System.Windows.Forms.Form
{
    private System.Windows.Forms.Button retrieveButton;
    private System.Windows.Forms.DataGrid dataGrid;

    /**////    <summary>
    ///    Construct the window.
    ///    </summary>
    ///    <remarks>
    ///    This method    constructs the window by creating both the data    grid and the button.
    ///    </remarks>
    public DisplayTabularData()
    {
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(464, 253);
        this.Text = "01_DisplayTabularData";
        this.dataGrid = new System.Windows.Forms.DataGrid();
        dataGrid.BeginInit();
        dataGrid.Location = new System.Drawing.Point(8, 8);
        dataGrid.Size = new System.Drawing.Size(448, 208);
        dataGrid.TabIndex = 0;
        dataGrid.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        this.Controls.Add(this.dataGrid);
        dataGrid.EndInit();
        this.retrieveButton = new System.Windows.Forms.Button();
        retrieveButton.Location = new System.Drawing.Point(384, 224);
        retrieveButton.Size = new System.Drawing.Size(75, 23);
        retrieveButton.TabIndex = 1;
        retrieveButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        retrieveButton.Text = "Retrieve";
        retrieveButton.Click += new System.EventHandler(this.retrieveButton_Click);
        this.Controls.Add(this.retrieveButton);
    }

    /**////    <summary>
    ///    Retrieve the data
    ///    </summary>
    ///    <param name="sender"> </param>
    ///    <param name="e"> </param>
    protected void retrieveButton_Click(object sender, System.EventArgs e)
    {
        retrieveButton.Enabled = false;

        string source = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/Manio/My Documents/PrintManager/program/PrintManageV1/DataBase/PrintDB.mdb";
        string select = "SELECT * FROM MainInfo";

        /**/////////////////////////////////
        //填充DataSet的步驟
        //1、使用數(shù)據(jù)庫(kù)連接字符串創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象
        //    2、用SQL查詢語(yǔ)句和數(shù)據(jù)庫(kù)連接對(duì)象創(chuàng)建數(shù)據(jù)庫(kù)適配器dataAdapter
        //        3、使用DataAdapter的Fill 方法填充DataSet

        OleDbConnection OleCon = new OleDbConnection(source);

        OleDbDataAdapter da = new OleDbDataAdapter(select,OleCon);

        DataSet ds = new DataSet();

        da.Fill(ds, "MainInfo");

        dataGrid.SetDataBinding(ds, "MainInfo");    //DataGrid的數(shù)據(jù)綁定,使用DataSet 和 數(shù)據(jù)庫(kù)的表名
    }

    /**////    <summary>
    ///    Display    the    application    window
    ///    </summary>
    static void Main()
    {
        Application.Run(new DisplayTabularData());
    }
}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 北海市| 新宾| 五常市| 上思县| 黑山县| 额尔古纳市| 武义县| 惠东县| 龙井市| 康平县| 陆良县| 信阳市| 定州市| 祁门县| 吴川市| 东光县| 丹凤县| 兰州市| 拉孜县| 彰武县| 同心县| 新蔡县| 锦屏县| 芜湖市| 呼图壁县| 山东省| 贵阳市| 铁岭市| 香港 | 永吉县| 饶河县| 昂仁县| 电白县| 临清市| 翁源县| 蓬溪县| 闻喜县| 新龙县| 突泉县| 商南县| 赤城县|