Sql Data Reader Sql Command command new Sql

  • Slides: 8
Download presentation

Sql. Data. Reader的使用 • Sql. Command command = new Sql. Command(); • command. Connection

Sql. Data. Reader的使用 • Sql. Command command = new Sql. Command(); • command. Connection = sql. Cnt; • command. Command. Type = Command. Type. Text; • command. Command. Text = "Select * from Goods"; • Sql. Data. Reader reader = command. Execute. Reader(); //执行SQL,返回一个“流” • while (reader. Read()) • { • • } Console. Write(reader[“goodname”]); // 打印出每个商品的名字

Sql. Data. Adapter的作用 • Sql. Data. Adapter是Sql. Command和Data. Set之间的桥梁; • 创建Sql. Data. Adapter的步骤是: •

Sql. Data. Adapter的作用 • Sql. Data. Adapter是Sql. Command和Data. Set之间的桥梁; • 创建Sql. Data. Adapter的步骤是: • Sql. Data. Adapter my. Data. Adapter = new Sql. Data. Adapter(); • my. Data. Adapter. Select. Command = my. Sql. Command; // 为Sql. Data. Adapter对象绑定所要执行的 Sql. Command对象 • my. Data. Adapter. fill():将执行结果填充到Dataset中,会隐藏打开Sql. Connection并执行SQL等 操作。

使用Data. Set • 首先应该将Sql. Data. Adapter返回的数据集(表)填充到Dataset对象中: • Sql. Data. Adapter my. Data. Adapter =

使用Data. Set • 首先应该将Sql. Data. Adapter返回的数据集(表)填充到Dataset对象中: • Sql. Data. Adapter my. Data. Adapter = new Sql. Data. Adapter("select * from goods", sql. Cnt); • Data. Set my. Data. Set = new Data. Set(); // 创建Data. Set • my. Data. Adapter. Fill(my. Data. Set, “goods");

访问Data. Set中的数据 • Data. Table my. Table = my. Data. Set. Tables[“goods"]; • foreach

访问Data. Set中的数据 • Data. Table my. Table = my. Data. Set. Tables[“goods"]; • foreach (Data. Row my. Row in my. Table. Rows) { • foreach (Data. Column my. Column in my. Table. Columns) { • Console. Write. Line(my. Row[my. Column]); //遍历表中的每个单元格 • } • }

THANK YOU

THANK YOU