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

首頁(yè) > 編程 > .NET > 正文

Programming in ADO.NET by Andrew R. Phillips

2024-07-10 13:03:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
programming in ado.net by andrew r. phillipssubmitted byuser leveldate of submissionandrew r. phillipsintermediate03/19/2001
programming in  c# with ado.net is much easier than it sounds. microsoft has made it simpler for anyone to connect. ado.net has  the adodata class and if you are using a sql server  database you can use sqldataset classed for betrer speed and performance.
the first thing you need to do is add the following code to the top of the code form.
using system.data;  
the next thing you need to do is to add a ado connection resource and an ado command resource.  you can do this two ways.  the first way is from the form.cs(design) open up the toolbox click on the data components and drag and drop an adoconnection and adodatasetcommand objects into the form.
the following code will be added to your code form.  
private system.data.ado.adodatasetcommand adodatasetcommand1;
private system.data.ado.adoconnection adoconnection1;
next you need to make a connection to the database using the adoconnection and th adodatasetcommand objects.  the connection  and command string  will look like this:  
string sconn = "server=localhost;uid=sa;pwd=;database=northwind";
string scommand ="select firstname, lastname from employees";
you then need to connect to the database.  this is is done by calling a new adodatasetcommand object using the command and connection string as the parameters.  
adodatasetcommand dscmd = new adodatasetcommand(scommand, sconn);
so far the ado.net has look a lot like the ado we have come to love and adore.  once we have made the database connection and have queried the database we are ready for something new. the dataset object.  the dataset object.  the dataset object is more than just an expanded recordset.  with the dataset object you connect to a copy of a database table(s) or even an entire database.  also you are able to read and write to a xml files.

by calling filldataset() of the adodatasetcommand you are able to load a dataset with data from a database.  


dataset ds = new dataset();
dscmd.filldataset(ds);   
next you need to create a new datatable and connecting it to your dataset, by using the method tables of the dataset class.  datatable class has several other methods two of which are columns and constraints, which returns a collection of columns and constraints respectively.  
datatable dt = ds.tables["employees"];  

you are now ready to get the data from the database.  you can use the  foreach function along with a datarow object to access the data quite easily.


foreach(datarow dr in dt.rows)
{
      listbox1.items.add(dr["firstname"] + ":" +
      dr["lastname"]};
}

to put the data into a grid all you need to do is to create a data grid in the design mode and then after the  dscmd.filldataset(ds, 揈mployees?; use the following code

datagrid1.datasource=ds.tables[揈mployees擼.defaultview; ]


this will put the data from the dataset directly into the datagrid.
the complete code:.
namespace windowsapplication4
{
    using system;
    using system.drawing;
    using system.collections;
    using system.componentmodel;
    using system.winforms;
    using system.data;
    /// <summary>
    ///    summary description for form1.
    /// </summary>
    public class form1 : system.winforms.form
    {
       /// <summary>
        ///    required designer variable.
        /// </summary>
        private system.componentmodel.container components;
        private system.data.ado.adodatasetcommand adodatasetcommand1;
        private system.data.ado.adoconnection adoconnection1;
        private system.winforms.listbox listbox1;  
        public form1()
        {
            //
            // required for windows form designer support
            //
            initializecomponent();
                  dataset ds = new dataset();
                  adodatasetcommand1.filldataset(ds);
                  datatable dt = ds.tables["treasury"];
                  foreach(datarow dr in dt.rows)
                  {
                        listbox1.items.add(dr["base_last_updated"] + ":" +
                              dr["base_yield"] + " " +dr["coupon"]);
                 }
      },歡迎訪問(wèn)網(wǎng)頁(yè)設(shè)計(jì)愛(ài)好者web開(kāi)發(fā)。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 福州市| 旬阳县| 西宁市| 长垣县| 麦盖提县| 黔西县| 兴安县| 新郑市| 黄冈市| 独山县| 当阳市| 平阳县| 伽师县| 云阳县| 旌德县| 凤冈县| 靖宇县| 随州市| 渝中区| 绥棱县| 那曲县| 依安县| 怀化市| 阿克| 宝山区| 乾安县| 永兴县| 金平| 建阳市| 连南| 庆城县| 乳山市| 普陀区| 桃源县| 奇台县| 交城县| 哈尔滨市| 民丰县| 鲜城| 松潘县| 博兴县|