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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python dataset用法_dataset 用法(2)

發布時間:2023/12/10 python 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python dataset用法_dataset 用法(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、為DataTable添加列

(1)添加列

DataTable ?tbl = ds.Tables.Add("User");

DataColumn col =tbl.Columns.Add("UserID",typeof(int));

col.AllowDBNull = false;

col.MaxLength = 6;

col.Unique = true;

tbl.PrimaryKey = new DataColumn[]{tbl.Columns["UserID"]};

當設置主鍵時,AllowDBNull自動設置為False;

(2)添加自增列

DataSet ds = new DataSet();

DataTable tbl = ds.Tables.Add("User");

DataColumn col = tbl.Columns.Add("UserID",typeof(int));

col.AutoIncrement = true;

col.AutoIncrementSeed = -1;

col.AutoIncrementStep = -1;

col.ReadOnly = true;

2、修改DataTable中的數據

(1)添加數據行

DataRow row = ds.Tables["User"].NewRow();

row["UserID"] = "123456";

ds.Tables["User"].Rows.Add(row);

object[] aValues ={"123456","張三"}

da.Tables["User"].LoadDataRow(aValues,false);

(2)修改當前行

修改行的內容 Datast并不會自動修改數據庫中相應的內容,而是要使用SqlDataAdapter對象的Update方法來提交修改。

//查找UserID為123456的用戶,有則修改

DataRow ? User;

User = ds.Tables["UserID"].Rows.Find("123456");

if(User == null)

//沒有查找客戶

else

{

User.BeginEdit();

User["UserName"] ="王五";

User.EndEdit();

}

obejct[] ?User ={null,”王五”} ;//null表示不修改該列的數據

DataRow rowUser;

rowUser = ds.Tables["UserID"].Rows.Find("123456");

rowUser.ItemArray = User;

(3)處理行中的空值

A:查看是否為空

DataRow rowUser;

rowUser = ds.Tables["UserID"].Rows.Find("123456");

if(rowUser.IsNull("Address"))

Console.WriteLine("Address is Null");

else

Console.WriteLine("Address is not Null");

B:賦予空值

rowUser["Address"] = DBNull.Value;

(4)刪除行

DataRow rowUser;

rowUser = ds.Tables["UserID"].Rows.Find("123456");

rowUser.Delete();

(5)清除DataRow

A:

DataRow rowUser;

rowUser = ds.Tables["UserID"].Rows.Find("123456");

rowUser.ItemArray = aUser;

da.Tables["User"].Remove(rowUser);

B:

ds.Tables["User"].RemoveAt(intIndex);

3、實例

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)

{

//創建連接

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());

}

SqlCommandBuilder mybuilder = new SqlCommandBuilder(adapt);

//創建DataSet

DataSet ds = new DataSet();

//將數據添加到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],

ds.Tables["mytest"].Columns[3], ds.Tables["mytest"].Columns[4]);

//輸出DataSet中的所有數據

Console.WriteLine("before up data");

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

{

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

row[1] , row[2] , row[3]);

}

//將第一行的第四列的值修改為123

DataRow rows1 = ds.Tables["mytest"].Rows[0];

rows1[3] = "123";

//刪除該行

rows1.Delete();

//插入一行

DataRow newrow = ds.Tables["mytest"].NewRow();

newrow[0] = "mmm";

newrow[1] = 36;

newrow[2] = "aaa";

newrow[3] = 222;

ds.Tables["mytest"].Rows.Add(newrow);

//在DataSet中查找數據

DataColumn[] keys = new DataColumn[2];

keys[0] = ds.Tables["mytest"].Columns["name"];

keys[1] = ds.Tables["mytest"].Columns["number"];

//keys[1] = ds.Tables["mytest"].Columns ["type"];

ds.Tables["mytest"].PrimaryKey = keys;

DataRow findrow = ds.Tables["mytest"].Rows.Find("rrr");

if (findrow != null)

{

Console.WriteLine("{0}is find in tables",findrow);

//delete the row

Console.WriteLine("removing the rows ........");

findrow.Delete();

}

//用DataSet中的數據更新表

adapt.Update(ds,"mytest");

//輸出DataSet中的所有數據

Console.WriteLine("after up data");

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

{

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

row[1], row[2], 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,type,low,high From dbo.spt_values WHERE number=36";

Program sqlcon = new Program();

sqlcon.SQLConnectionF(sou, sel);

}

}

}

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。