国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 枣阳市| 兴文县| 岫岩| 泾源县| 尼勒克县| 轮台县| 襄樊市| 盘锦市| 镇宁| 惠安县| 尉氏县| 甘孜县| 花莲市| 大厂| 义马市| 霞浦县| 望奎县| 崇义县| 滦平县| 合山市| 邵武市| 水富县| 乌兰察布市| 黄梅县| 安康市| 芜湖市| 临武县| 凤山县| 喀什市| 阳春市| 那坡县| 新郑市| 远安县| 金山区| 侯马市| 湖口县| 长武县| 土默特右旗| 延寿县| 项城市| 绵阳市|