zoukankan      html  css  js  c++  java
  • 使用类型化数据集与非类型化数据集完成用户信息的增删查改(作业整理)

    期末因为时间赶得紧,做的很不好。原想加入事件、存储过程等后来都没加进去。而且还得帮着其他同学做作业,自己的作业做的还有很多地方需要完善的。

    一、采用非类型化数据集完成

    using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;


    namespace ADO.NET作业二采用非类型化数据集
    {
    class Program
    {
    static void Main(string[] args)
    {
    //采用非类型化数据集
    DataSet ds = new DataSet();
    //string str = ConfigurationManager.ConnectionStrings["sqlstring"].ConnectionString;
    string str = "Data Source=.;" + "Initial Catalog=BookDB;" + "Integrated Security=True";
    try
    {
    using (SqlConnection sqlcon = new SqlConnection(str))
    {
    SqlCommand cmd = sqlcon.CreateCommand();
    cmd.CommandText = "select * from Books";
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    adapter.Fill(ds,"Books");

    //1、修改第一条记录,修改后的记录为(1,操作系统,35,清华大学出版社)
    ds.Tables["Books"].Rows[0]["BookPrice"] = "35";
    ds.Tables["Books"].Rows[0]["BookPress"] = "清华大学出版社";

    //2、插入一条记录(4,数据结构,32.5,高教出版社)。
    DataRow dr = ds.Tables["Books"].NewRow();

    dr["BookId"]= 4;
    dr["BookTitle"] = "数据结构";
    dr["BookPrice"] = 32.5F;
    dr["BookPress"] = "高教出版社";
    ds.Tables["Books"].Rows.Add(dr);

    foreach (DataRow row in ds.Tables["Books"].Rows)
    {
    foreach (DataColumn column in ds.Tables["Books"].Columns)
    {
    Console.WriteLine("{0} Current={1}",column.ColumnName,row[column,DataRowVersion.Current]);
    Console.WriteLine("Default ={0}",row[column,DataRowVersion.Default]);
    //Console.WriteLine("Original={0}", row[column, DataRowVersion.Original]);
    //因为新添加的行没有原始值所以需要判断
    if (dr["BookId"].Equals(4))
    {
    Console.WriteLine("{0} Current={1}", column.ColumnName, row[column, DataRowVersion.Current]);
    Console.WriteLine("Default ={0}", row[column, DataRowVersion.Default]);

    /*if (column.ColumnName == "BookId" && dr["BookId"].Equals(4) )
    {
    Console.WriteLine("{0} Current={1}", column.ColumnName, row[column, DataRowVersion.Current]);
    Console.WriteLine("Default ={0}", row[column, DataRowVersion.Default]);
    }*/

    }
    else
    {
    Console.WriteLine("Original={0}",row[column,DataRowVersion.Original]);
    }

    Console.WriteLine();
    }
    }

    Console.WriteLine("删除第三条记录并输出其状态:");
    Console.WriteLine();
    //3、删除第三条记录.
    ds.Tables["Books"].Rows[2].Delete();
    Console.WriteLine("{0}", ds.Tables["Books"].Rows[2].RowState);
    }
    }
    catch(SqlException e)
    {
    Console.WriteLine("{0}",e.Message);
    }
    Console.ReadLine();
    }
    }
    }

    //DataRow[] rows = ds.Tables[0].Select("name='张三'");

    //查找

    //Response.Write(rows[0]["name"].ToString()+rows[0]["password"]);

    //DataRow []rows=t.Select("Dname='"+TextBox3 .Text .Trim ()+"'","id DESC");

    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

    adapter.Update(ds, "Users");//这句话必须有否则只能更新视图不能更新数据库

    this.GridView1.DataSource = ds.Tables[0];

    this.GridView1.DataBind();

    结果截图:

    image_thumb[5]

    二、采用类型化数据集完成

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Configuration;

    using System.Data;

    namespace ADO.NET作业

    {

    class Program

    {

    //采用类型化数据集

    static void Main(string[] args)

    {

    BookDBDataSetTableAdapters.BooksTableAdapter adapter =

    new BookDBDataSetTableAdapters.BooksTableAdapter();

    BookDBDataSet.BooksDataTable table=new BookDBDataSet.BooksDataTable();

    adapter.Fill(table);

    //1、修改第一条记录,修改后的记录为(1,操作系统,35,清华大学出版社)

    table [0].BookPrice=35;

    table[0].BookPress="清华大学出版社";

    //2、插入一条记录(4,数据结构,32.5,高教出版社)。

    BookDBDataSet.BooksRow row= table.NewBooksRow();

    row.BookId = 4;

    row.BookTitle="数据结构";

    row.BookPrice=32.5F;

    row.BookPress="高等教育出版社";

    table.Rows.Add(row);

    /*var queryResult=from r in table

    where (bool)r[2]

    select r;*/

    Console.WriteLine("输出以改变的数据表-未删除第三条记录");

    Console.WriteLine();

    foreach (DataRow dataRow in table.Rows)

    {

    foreach (DataColumn dataColumn in table.Columns)

    {

    Console.WriteLine("{0} Current= {1}",dataColumn.ColumnName,

    dataRow[dataColumn,DataRowVersion.Current]);

    Console.WriteLine("Default={0}", dataRow[dataColumn, DataRowVersion.Default]);

    if (row.BookId.Equals(4))

    {

    Console.WriteLine("{0} Current= {1}", dataColumn.ColumnName,

    dataRow[dataColumn, DataRowVersion.Current]);

    Console.WriteLine("Default={0}", dataRow[dataColumn, DataRowVersion.Default]);

    }

    else

    {

    Console.WriteLine("Original={0}", dataRow[dataColumn, DataRowVersion.Original]);

    }

    }

    }

    Console.WriteLine("删除第三条记录并输出其状态:");

    Console.WriteLine();

    //3、删除第三条记录

    table[2].Delete();

    Console.WriteLine("第三条记录的状态:{0}", table[2].RowState);

    Console.ReadLine();

    }

    }

    }

    DataRow[] rows = table.Select("name='张三'"); //查找

    Response.Write(rows[0]["uid"] + " | " + rows[0]["name"].ToString() + " | " + rows[0]["password"] + " | " + rows[0]["power"] + " | " + rows[0]["lock"]);

    adapter.Update(table); //这句话必须有否则只能更新视图不能更新数据库

    this.GridView1.DataSource = table;

    this.GridView1.DataBind();

     

    结果截图:

    image_thumb[8]

    image_thumb[11]

    道在我心,一以贯之 HanWang
  • 相关阅读:
    HDOJ1556 Color the ball
    BUPT( 北邮)2 大数 AB
    DateTime.ToString()的用法
    判断DataTable中的空值(字段为数值型)?
    [转].NET平台下的Excel编程|C#操作Excel|Application和ApplicationClass的联系和区别
    [转]寻找SqlHelper
    C#认识/理解/运用 StreamReader,StreamWriter,StringReader,StringWriter[转]
    C# 应用微软的Visual Studio International Pack 类库提取汉字拼音首字母[转]
    [转]想靠写程序赚更多钱,写到两眼通红,写得比别人都又快又好好几倍,结果又能如何?
    DataRow复制一行到另一个DataTable[转]
  • 原文地址:https://www.cnblogs.com/wshcn/p/2367203.html
Copyright © 2011-2022 走看看