我爲我的Gridview使用ObjectDataSource。 我用來返回值的函數只是返回表中最後一個值。 我做了哪些更改以返回所有值。只返回1個值的對象數據源函數
public class Employees
{
public int e_number;
public string e_name;
public string e_designation;
private SqlConnection conn = null;
private SqlCommand cmd = null;
private string constring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public Employees()
{
conn = new SqlConnection(constring);
cmd = new SqlCommand();
}
public int Employee
{
get
{
return e_number;
}
set
{
e_number = value;
}
}
public string Name
{
get
{
return e_name;
}
set
{
e_name = value;
}
}
public string Designation
{
get
{
return e_designation;
}
set
{
e_designation = value;
}
}
public Employees GetEmployee()
{
string strquery = "select [Number],[Name],[Designation] from [Users]";
conn.Open();
cmd.Connection = conn;
cmd.CommandText = strquery;
SqlDataReader objSqlDataReader = cmd.ExecuteReader();
int counter = 0;
// ArrayList myObj = new ArrayList();
// Employees objEmployees = new Employees();
Employees emp = null;
//if (objSqlDataReader.Read())
while(objSqlDataReader.Read())
{
emp = new Employees();
// myObj[counter] = new Employees();
// Employees employee = new Employees();
emp.Employee = (int)objSqlDataReader["Number"];
emp.Name=(string)objSqlDataReader["Name"];
emp.Designation = (string)objSqlDataReader["Designation"];
// objEmployees.e_number = (int)objSqlDataReader["Number"];
// objEmployees.e_name = (string)objSqlDataReader["Name"];
//objEmployees.e_designation = (string)objSqlDataReader["Designation"];
}
conn.Close();
return emp;
//return objEmployees;
}
}
}
我已經嘗試了所有的方法,如製作對象的數組,但我無法。 請你能告訴我在我的代碼中添加什麼。
你應該有隻有屬性單獨Employee類和其他類EmployeeDataAccess這確實CRUD操作 – 2012-04-20 14:07:21