Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于填充结构的SqlDataReader_C#_Sql Server_Struct_Visual Studio 2013_Sqldatareader - Fatal编程技术网

C# 用于填充结构的SqlDataReader

C# 用于填充结构的SqlDataReader,c#,sql-server,struct,visual-studio-2013,sqldatareader,C#,Sql Server,Struct,Visual Studio 2013,Sqldatareader,有一个私有的“Employee”类对象,它镜像了公共结构“empStruct”。我需要使用SQLReader从查询中读取行值来填充结构,然后将对象设置为与结构值相等。我认为有一个更简单的方法,但我对这一点非常陌生 public struct empStruct { public int eid; public string lastname; public string firstname; public DateTime

有一个私有的“Employee”类对象,它镜像了公共结构“empStruct”。我需要使用SQLReader从查询中读取行值来填充结构,然后将对象设置为与结构值相等。我认为有一个更简单的方法,但我对这一点非常陌生

public struct empStruct
    {
        public int eid;
        public string lastname;
        public string firstname;
        public DateTime birthdate;
        public DateTime hiredate;
        public bool ishourly;
        public decimal payrate;
    }
        public static bool SelectEmployee(int eid)
    {
        empStruct SelectRecord = new empStruct();
        Employee newEmp = new Employee();
        string sqlText;

        sqlText = "SELECT EID,EID, LastName, FirstName, BirthDate, HireDate, IsHourly, PayRate ";
        sqlText += "FROM Employee ";
        sqlText += "WHERE EID = @EID ";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {

            SqlCommand command = new SqlCommand(sqlText, connection);
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@EID", eid);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            //Call Read before accessing data.
            while (reader.Read())
            {
                ???
            }
            newEmp = SelectRecord;

        }

我知道“//Call Read…”之后的一切都是不完整的,我不知道这个阅读器是如何工作的

像这样加载结构

while (reader.Read())
{
newEmp.eid = (int)reader("EID");
newEmp.firstname = (string)reader("FirstName");
....
}
while (reader.Read())            {
   newEmp.lastname = reader.GetString(1);
   newEmp.firstname  = reader.GetString(2);


}

你能详细说明一下这是如何回答这个问题的吗?