c# ADO

 

刚才复习了一下ADO.NET,做了一个简单例子,加图片,以后可以没事回头复习看看

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace WindowsFormsDataTableView   
  11. {   
  12.     public partial class MainForm : Form   
  13.     {   
  14.         List<Car> listcars = new List<Car>();   
  15.         //创建   
  16.         DataTable invertoryTable = new DataTable("Inventroy");   
  17.         DataView coltsOnlyView;   
  18.         //主表单   
  19.         public MainForm()   
  20.         {   
  21.             InitializeComponent();   
  22.             listcars.Add(new Car("chucky","Bmw","green"));   
  23.             listcars.Add(new Car("Tiny""Yugo""white"));   
  24.             listcars.Add(new Car("ami""Jeep""tan"));   
  25.             listcars.Add(new Car("pain inducer""caravan""pink"));   
  26.             listcars.Add(new Car("fred""Bmw""pea soap green"));   
  27.             listcars.Add(new Car("sidd""Bmw""black"));   
  28.             listcars.Add(new Car("mel""Firebind""red"));   
  29.             listcars.Add(new Car("sarah""colt""black"));   
  30.             //创建一个表单;   
  31.             createDataTable();   
  32.             //创建一个视图;   
  33.             createDataView();   
  34.   
  35.         }   
  36.         //显示行号,没有实现。。。   
  37.         void carInventoryGridVIew_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)   
  38.         {   
  39.             using (SolidBrush b = new SolidBrush(Color.Black))   
  40.             {   
  41.                 e.Graphics.DrawString((e.RowIndex).ToString(),   
  42.                 e.InheritedRowStyle.Font, b,   
  43.                 e.RowBounds.Location.X + 15,   
  44.                 e.RowBounds.Location.Y + 4);   
  45.             }   
  46.         }   
  47.         //创建制造商为Colt的视图   
  48.         void createDataView()   
  49.         {   
  50.             coltsOnlyView =new DataView(invertoryTable);   
  51.             coltsOnlyView.RowFilter = "Make = 'Colt'";   
  52.             dataGridColtsVIew.DataSource=coltsOnlyView;   
  53.         }   
  54.         //创建图表   
  55.         void createDataTable()   
  56.         {   
  57.             DataColumn carIDColumn = new DataColumn("CarID"typeof(int));   
  58.             carIDColumn.Caption = "Car ID";   
  59.             carIDColumn.ReadOnly = true;   
  60.             carIDColumn.AllowDBNull = false;   
  61.             carIDColumn.Unique = true;   
  62.             carIDColumn.AutoIncrement = true;   
  63.             carIDColumn.AutoIncrementSeed = 0;   
  64.             carIDColumn.AutoIncrementStep = 1;   
  65.             DataColumn carMakeColumn = new DataColumn("Make"typeof(string));   
  66.             DataColumn carColorColumn = new DataColumn("Color"typeof(string));   
  67.             DataColumn carPetNameColumn = new DataColumn("PetName"typeof(string));   
  68.             carPetNameColumn.Caption = "Pet Name";   
  69.             invertoryTable.Columns.AddRange(new DataColumn[] { carIDColumn,carMakeColumn, carColorColumn, carPetNameColumn });   
  70.             invertoryTable.PrimaryKey = new DataColumn[] { invertoryTable.Columns[0] };   
  71.             foreach (Car c in listcars)   
  72.             {   
  73.                 DataRow newRow = invertoryTable.NewRow();   
  74.                 newRow["Make"] = c.carMake;   
  75.                 newRow["Color"] = c.carColor;   
  76.                 newRow["PetName"] = c.carPetName;   
  77.                 invertoryTable.Rows.Add(newRow);   
  78.             }   
  79.             carInventoryGridVIew.DataSource = invertoryTable;   
  80.         }   
  81.         //删除某一行Button   
  82.         private void BtnRemoverRow_Click(object sender, EventArgs e)   
  83.         {   
  84.             try  
  85.             {   
  86.                 invertoryTable.Rows[(int.Parse(txtRowRemove.Text))].Delete();   
  87.                 invertoryTable.AcceptChanges();   
  88.             }   
  89.             catch (Exception ex)   
  90.             {   
  91.                 MessageBox.Show(ex.Message);   
  92.             }   
  93.         }   
  94.         //显示耨一制造商Button   
  95.         private void btnDisplayMakes_Click(object sender, EventArgs e)   
  96.         {   
  97.             string filterstr = string.Format("Make= '{0}'", txtMakeToView.Text);   
  98.             DataRow[] makes = invertoryTable.Select(filterstr,"PetName DESC");   
  99.             if (makes.Length == 0)   
  100.                 MessageBox.Show("Sorry, no cars...""Selection error!");   
  101.             else{   
  102.                 string strmake =null;   
  103.                 for(int i=0;i<makes.Length;i++)   
  104.                 {   
  105.                     DataRow temp =makes[i];   
  106.                     strmake += temp["PetName"]+"/n";   
  107.                 }   
  108.                 MessageBox.Show(strmake,string.Format("{0} types:",txtMakeToView.Text));   
  109.             }   
  110.         }   
  111.         //显示ID号大于5的汽车名字和ID号方法   
  112.         private void showcarswithIDgreaterthanfive()   
  113.         {   
  114.             DataRow[] properIDs;   
  115.             string newfilterstr = "CarID >5";   
  116.             properIDs = invertoryTable.Select(newfilterstr);   
  117.             string striDs = null;   
  118.             for(int i=0;i<properIDs.Length;i++)   
  119.             {   
  120.                 DataRow temp = properIDs[i];   
  121.                 striDs += temp["PetName"] + "is ID:" + temp["CarID"] + "/n";   
  122.             }   
  123.             MessageBox.Show(striDs, "Pet names of cars where CarID >5");   
  124.         }   
  125.         //显示ID号大于5的Button   
  126.         private void btnshowcar_Click(object sender, EventArgs e)   
  127.         {   
  128.             showcarswithIDgreaterthanfive();   
  129.         }   
  130.         //更改BMWtoYUGOS的Button   
  131.         private void btnchangeemwtoyugos_Click(object sender, EventArgs e)   
  132.         {   
  133.             if (DialogResult.Yes == MessageBox.Show("Are you sure?? Bmws are much nicer than Yugos!",   
  134.                 "Please Confirm!", MessageBoxButtons.YesNo))   
  135.             {   
  136.                 string filterstr = "Make ='Bmw'";   
  137.                 string strMake = string.Empty;   
  138.                 DataRow[] makes = invertoryTable.Select(filterstr);   
  139.                 for (int i = 0; i < makes.Length; i++)   
  140.                     makes[i]["Make"] = "Yugo";   
  141.             }   
  142.         }   
  143.   
  144.     }   
  145.   
  146.     }   
  147.   
  148.     //汽车类,包含汽车ID,汽车名,制造商,汽车颜色    
  149.     class Car   
  150.     {   
  151.         public int carID   
  152.         { getset; }   
  153.         public string carPetName   
  154.         { getset; }   
  155.         public string carMake   
  156.         { getset; }   
  157.         public string carColor   
  158.         { getset; }   
  159.         public Car(string petName, string make, string color)   
  160.         {   
  161.             carPetName = petName;   
  162.             carMake = make;   
  163.             carColor = color;   
  164.         }   
  165.     }   
  166.   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值