示例代码1:(DataTable中包含多列数据的情况)
public Customer[] GetAll()
{
DataTable table = SqlHelper.ExecuteDataTable("select FId, FName, FBirthday, FCustLevel, FTelNum ,FAddress from T_Customer "); //得到一个DataTable对象
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
//把下面的对象封装成一个方法
Customer customer = new Customer();//得到一个客户类对象
//对得到的客户类对象进行封装
customer.ID=(int)row["FId"];
customer.Name=(string)row["FName"];
customer.Birthday = (DateTime?)CommonHelper.FromDbValue(row["FBirthday"]);
customer.CustLevel=(int)row["FCustLevel"];
customer.TelNum=(string)row["FTelNum"];
customer.Address=(string)row["FAddress"];
customers[i] = customer;//把封装的Model对象赋值给Model数组对象的第 i 个元素
return customers;
}
示例代码2:(DataTable中包含一列数据的情况)
public TableName[] GetAllNames()
{
DataTable table = DAL.SqlHelper.ExecuteDataTable(@"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'");//拿到一个DataTable数据表对象
string[] tables = new string[table.Rows.Count];//通过表中的行的数目,得到一个表的行数的字符串数组,因为数组中只包含一个字符串类型的参数TABLE_NAME,所以数组类型设置成string类型。如果是数据行中的数据类型是int类型,则要把这里的数组类型设置成int类型
for (int i = 0; i < table.Rows.Count; i++)//以DataTable对象长度为界限进行一个遍历
{
DataRow row = table.Rows[i];//得到Dable中的每一个行Row
tables[i] = (string)row["TABLE_NAME"];//把每一个行中的TABLE_NAME列赋值给字符串数组中的每一个元素
}
return tables;
}