.net 操作DataTable

来源:互联网 发布:阿里云centos镜像下载 编辑:程序博客网 时间:2024/05/22 15:40
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;namespace DataTableSample{    class Program    {        static void Main(string[] args)        {            DataTable dtProduct = new DataTable("Product");            DataSet ds = new DataSet();                       DataColumn dc1 = new DataColumn ("id",Type.GetType("System.String"));            DataColumn dc2 = new DataColumn("name",Type.GetType("System.String"));            dtProduct .Columns.Add (dc1);            dtProduct .Columns .Add (dc2);            ds.Tables.Add(dtProduct);            dc1.MaxLength =10;            dc2.MaxLength =30;            dc1.AllowDBNull =false ;            dc1.Unique =true ;            dtProduct.PrimaryKey = new DataColumn[] { dc1 };            //添加行方法            DataRow dr = dtProduct.NewRow();//创建的新行            dr["id"] = Guid.NewGuid().ToString();//通过列名设置列值            dr[1] = "默认";//设置列值,通过列索引,从0开始            dtProduct.Rows.Add(dr);            DataRow dr1 = dtProduct.NewRow();            dtProduct.Rows.Add(dr1);            //查找行            DataRow[] drs = dtProduct.Select("id is null");            DataRow[] drs1 = dtProduct.Select("id is not null");            //操作行            DataRow cdr = dtProduct.Rows[1];            cdr[0] = Guid.NewGuid().ToString();            cdr[1] = "net";            //排序            dtProduct.DefaultView.Sort = "id desc";            dtProduct = dtProduct.DefaultView.ToTable();        }           }}
原创粉丝点击