日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dataset基本用法

發(fā)布時(shí)間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dataset基本用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用方法

1、創(chuàng)建DataSet對象

DataSet ds = new DataSet();

DataSet ds = new DataSet("DataSetName");

2、用數(shù)據(jù)集填充DataSet

最常用的是DataAdapter對象的Fill()方法給他填充數(shù)據(jù)

(1)

DataSet ds = new DataSet();

SqlDataAdapter adapt = new SqlDataAdapter(sqlcmd,con)

adapt.Fill(ds,"mytest");

(2)

DataSet ds=new DataSet();

DataTable dt=new DataTable("newTable");

ds.Tables.Add(dt);

(3)

DataSet ds=new DataSet();

DataTable dt=ds.Tables.Add("newTable");

3、訪問DataSet中的表、行和列 值

(1): 訪問每個(gè) DataTable

按表名訪問:ds.Tables["mytest"] //指定DataTable對象mytest(即訪問DataSet中名為mytest的DataTable)

按索引(索引基于0的)訪問:ds.Tables[0] //指定DataSet中的第一個(gè)DataTable

(2): 訪問DataTable中的行

ds.Tables["mytest"].Rows[n] //訪問mytest表 的第n+1行(行的索引是從0開始的)

ds.Tables[i].Rows[n] //訪問DataSet中的第i+1個(gè)DataTable 的第n+1列(列的索引是從0開始的)

(3): 訪問DataTable中的某個(gè)元素

ds.Tables["mytest"].Rows[n][m] //訪問mytest表的第n+1行第m+1列的元素

ds.Tables[i].Rows[n][m] //訪問DataSet中的第i+1個(gè)DataTable 表的第n+1行第m+1列的元素

ds.Tables["mytest"].Rows[n][name] //訪問mytest表的第n+1行name列的元素

ds.Tables[i].Rows[n][name] //訪問DataSet中的第i+1個(gè)DataTable 表的第n+1行name列的元素

(4): 取DataTable中的列名

ds.Tables["mytest"].Columns[n] //取出mytest表的n+1列列名

ds.Tables[i].Columns[n]

4、實(shí)例

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.SqlClient;

using System.Data;

namespace sqlconnection1

{

class Program

{

private void SQLConnectionF(string source, string select)

{

//創(chuàng)建連接

SqlConnection con = new SqlConnection(source);

SqlDataAdapter adapt = new SqlDataAdapter(select,con);

try

{

con.Open();

Console.WriteLine("connection is successful!");

}

catch (Exception e)

{

Console.WriteLine("connection error is :{0}", e.ToString());

}

//創(chuàng)建DataSet

DataSet ds = new DataSet();

//將數(shù)據(jù)添加到DataSet中

adapt.Fill(ds,"mytest");

//取出mytest表各列名

Console.WriteLine("{0,-15} {1,-10} {2,-10}",ds.Tables["mytest"].Columns[0],

ds.Tables["mytest"].Columns[1],ds.Tables["mytest"].Columns[2]);

//輸出mytest表中第六行

DataRow row1 = ds.Tables["mytest"].Rows[5];

Console.WriteLine("{0,-15} {1,-10} {2,-10}",row1[0],row1[1],row1[2]);

//輸出mytest表中第五行的第二列的值

DataRow row2 = ds.Tables["mytest"].Rows[4];

Console.WriteLine(" {0,-25} ", row2[1]);

//下列兩種方法等效都等同于row2[1](即第五行的第二列的值)

Console.WriteLine(" {0,-25} ", ds.Tables["mytest"].Rows[4][1]);

Console.WriteLine(" {0,-25} ", ds.Tables["mytest"].Rows[4]["number"]);

//輸出DataSet中的所有數(shù)據(jù)

foreach (DataRow row in ds.Tables["mytest"].Rows)

{

Console.WriteLine("{0,-15} {1,-10} {2,-10} {3}",row["name"] ,

row["number"] , row["low"] , row["high"]);

//取第三列的值

Console.WriteLine("{0,-15} ", row[3]);

}

Console.ReadLine();

con.Close();

}

static void Main(string[] args)

{

string sou = "server=duanyf\\SQLEXPRESS;" + "Initial Catalog=master;" + "UID = sa;" + "Password = dyf123";

string sel = "SELECT name,number,low,high From dbo.spt_values";

Program sqlcon = new Program();

sqlcon.SQLConnectionF(sou, sel);

}

}

}



另外一種用法

DataSet?result?=?new?DataSet();//定義DataSet對象,是一種存在內(nèi)存里的數(shù)據(jù)集合 result?=?new?Class1().hsggetdata(sql);//調(diào)用Class1這個(gè)類里的hsggetdata()函數(shù)給result賦值。 if?(result?!=?null)//如果result不為空 { ????if?(result.Tables[0].Rows.Count?>?0)//如果result里的第一張表的行數(shù)大于0 ????{ ????????Session["username"]?=?TextBox1.Text.ToString().Trim();//就給Session對象里的username賦值。 ????????if?(cx.Text.ToString().Trim()?==?"管理員")//判斷cx這個(gè)的顯示文字 ????????{ ????????????Session["cx"]?=?result.Tables[0].Rows[0]["cx"].ToString().Trim(); ????????} ????????else ????????{ ????????????Session["cx"]?=?cx.Text.ToString().Trim(); ????????????????????? ????????} ????????Response.Redirect("main.aspx"); ?????} ?????else ?????{ ?????????//執(zhí)行javascript代碼,在頁面彈框顯示這句話。。。。。 ?????????Response.Write("<script>javascript:alert('對不起,用戶名或密碼不正確,或您的帳號未經(jīng)審核!');</script>"); ????????????} ????????} ????????else ????????{ ????????????Response.Write("<script>javascript:alert('對不起,系統(tǒng)錯(cuò)誤,請不要越權(quán)操作!');</script>"); ????????} ????} ????protected?void?Button2_Click(object?sender,?EventArgs?e) ????{ ????????//點(diǎn)擊button2就執(zhí)行Button2_Click這個(gè)函數(shù),讓跳轉(zhuǎn)到y(tǒng)onghuzhuce_add.aspx這個(gè)頁面 ????????Response.Redirect("yonghuzhuce_add.aspx"); ????} } ??} ????????????else ????????????{ ????????????????Response.Write("<script>javascript:alert('對不起,用戶名或密碼不正確,或您的帳號未經(jīng)審核!');</script>"); ????????????} ????????} ????????else ????????{ ????????????Response.Write("<script>javascript:alert('對不起,系統(tǒng)錯(cuò)誤,請不要越權(quán)操作!');</script>"); ????????} ????} ????protected?void?Button2_Click(object?sender,?EventArgs?e) ????{ ????????Response.Redirect("yonghuzhuce_add.aspx"); ????} }

總結(jié)

以上是生活随笔為你收集整理的dataset基本用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。