首页 > 代码库 > c#datatable操作

c#datatable操作

c#操作datatable

1.创建表

DataSet ds=new DataSet();

DataTable dt=new DataTable("User");

ds.Add(dt);

dt.Columns.Add("user_name",typeof(string));

dt.Columns.Add("user_password",typeof(string));

dt.Columns.Add("user_page",typeof(int));

2.往表中添加数据

DataRow dr=dt.NewRow();

dr[0]="zhangsan";

dr[1]="123456";

dr[2]=25;

dt.Rows.Add(dr);


3.修改数据

           dataGridView1.DataSource = ds.Tables[0];
            DataTable dt = ds.Tables["user"];
            int i = dataGridView1.CurrentRow.Index;
            DataRow dr = dt.Rows[i];
            dr.BeginEdit();
            dr[0] = this.textBox1.Text;
            dr[1] = this.textBox2.Text;
            dr[2] = this.textBox3.Text;
            dr.EndEdit();

4.删除数据

            dataGridView1.DataSource = ds.Tables[0];
            DataTable dt = ds.Tables["user"];
            int i = dataGridView1.CurrentRow.Index;//获取当前行
            DataRow dr = dt.Rows[i];
            //dt.Rows.Remove(dr);//第二种删除操作用法
            dr.Delete();

 

c#操作DataTable 2
2008-05-26 16:43:18

c#操作DataTable

//创建datatable
        DataTable dt = new DataTable("king");

        //添加列方法1
        //添加一列列名为id,类型为string
        dt.Columns.Add("id", System.Type.GetType("System.String"));//直接为表创建一列
        //添加列方法
        //添加一列列名为foldername,类型为string
        DataColumn dc = new DataColumn("foldername", System.Type.GetType("System.String"));//定义一个列架构
        dt.Columns.Add(dc);//向表中插入列架构

        //添加行方法
        //添加有数据的行
        DataRow dr = dt.NewRow();//创建新列
        dr["id"] = Guid.NewGuid().ToString();//设置列值,通过列名
        dr[1] = "默认名片夹";//设置列值,通过列索引,从0开始
        dt.Rows.Add(dr);//想表中添加数据
        //添加空行
        DataRow dr1 = dt.NewRow();//创建新列
        dt.Rows.Add(dr1);//想表中添加数据

        //查找行
        DataRow[] drs = dt.Select("id is null");
        Response.Write(drs.Length.ToString()+"<br/>");
        DataRow[] drs1 = dt.Select("id is not null");
        Response.Write(drs1.Length.ToString() + "<br/>");
        Response.Write(drs1[0].ItemArray[0] + "_" + drs1[0].ItemArray[1] + "<br/>");//显示数据

        //操作行
        DataRow cdr = dt.Rows[1];
        cdr[0] = Guid.NewGuid().ToString();
        cdr[1] = "网络04G1";
        Response.Write(cdr.ItemArray[0] + "_" + cdr.ItemArray[1] + "<br/>");//显示数据

        //排序
        Response.Write(dt.Rows[0][0] + " | " + dt.Rows[0][1] + "_" + dt.Rows[1][0] + " | " + dt.Rows[1][1]+"<br>");
        dt.DefaultView.Sort = "id desc,foldername";
        dt = dt.DefaultView.ToTable();
        Response.Write(dt.Rows[0][0] + " | " + dt.Rows[0][1] + "_" + dt.Rows[1][0] + " | " + dt.Rows[1][1]);

ASP.NET基础教程-DataTable、DataRow、DataColumn对象的使用3

//创建并声明DataTable对象
DataTable total=new DataTable();
//给DataTable对象添加DataColumn对象
total.Columns.Add(new DataColumn("班组",typeof(string)));
total.Columns.Add(new DataColumn("姓名",typeof(string)));
total.Columns.Add(new DataColumn("学科",typeof(string)));
total.Columns.Add(new DataColumn("成绩",typeof(int)));
//创建并声明DataRow对象
DataRow dr=total.NewRow();
//为DataRow对象中的列进行赋值
dr["班组"]= "一班";
dr["姓名"]= " 李宏";
dr["学科"]= "语文 ";
dr["成绩"]=98;
//将DataRow对象添加到DataTable中
total.Rows.Add(dr);
dr=total.NewRow();
dr["班组"]= "一班";
dr["姓名"]= " 刘京生";
dr["学科"]= "语文 ";
dr["成绩"]=96;
total.Rows.Add(dr);
//在页面上输出DataTable对象中的数据
for(int i=0;i<total.Rows.Count;i++)
{
Response.Write(total.Rows[i][0].ToString()+"<br>");
Response.Write(total.Rows[i][1].ToString()+"<br>");
Response.Write(total.Rows[i][2].ToString()+"<br>");
Response.Write(total.Rows[i][3].ToString()+"<br>");
}
c#操作DataTbale 3:

 

  DataTable memTable = new DataTable( "tableName" );
            memTable.Columns.Add( new DataColumn( "ID", typeof( int ) ) );
            memTable.Columns.Add( new DataColumn( "Username", typeof( string ) );
            memTable.Columns.Add( new DataColumn( "Password", typeof( Guid ) );

            DataRow row = memTable.NewRow();
            row[ "ID" ] = 1;
            row[ "Username" ] = "badbug";
            row[ "Password" ] = Guid.NewGuid();

            memTable.Rows.Add( row ); 

c#datatable操作