初始化准备:
Employees emps = new Employees();
int empID;
1. 获取表中的所有数据行:
emps.LoadAll();
2. 根据主键获取一行数据:
emps.LoadByPrimaryKey(empID);
3. 插入一行:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上面是dOOdad文档给出的例子,但是我调用Save方法插入数据库没有问题,但是获取主键值会抛出异常。默认生成的代码没有将ID作为输出参数,所以emps.EmplyeeID仍然为空,不能转换成int。更正如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
4. 删除一行:
//先定位到要删除的行(参考6.12 设置当前行)
emps.MarkAsDeled();
emps.Save();
5. 更新一行:
//先定位到要更新的行
emps.LastName = "Jones";
emps.Save();
6. 获取表中的行数:
emps.RowCount;
7. 遍历表中所有的数据行:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
8. 设置排序表达式:
emps.Sort = Employees.ColumnNames.LastName + " DESC";
9. 设置过滤/选择表达式:
emps.Filter = Employees.ColumnNames.LastName + " LIKE A%";
10. 对列进行操作(只是在应用程序的DataSet中进行,修改不会被保存进数据库):
1) AddColumn
2) SetColumn
3) GetColumn
4) IsColumnNull
5) SetColumnNull
6) Example:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
11. 关于连接字符串:
上面讲到,可将连接字符串保存到配置文件并赋给dbConnection键值;其实可以在运行时对字符串进行赋值:
emps.ConnectionString = "User=me;Password=pw;Database=Employees;DataSource=MyServer";
12. 设置当前行:
当要修改或删除一行时,该行必须被指定为当前行,可以通过如下几种方法来实现:
1). LoadAll()或Query.Load():将当前行指向第一行;
2). LoadByPrimaryKey:将当前行指向返回的行;
3). AddNew() 在插入数据库之前,当前行指向正在操作的新行;
4). Rewind()和MoveNext():Rewind()后当前行指向第一行,MoveNext()移动到下一行;