我从我的数据库表中检索4个字段。现在我想将它们添加到动态列表<>。如何将这些添加到列表中。 我尝试这样做:类型类的列表,将值添加到数据类型的List <>类CLASS
public class myclass
{
public string StdDetails, StdAdderID;
public DateTime StdAddedDate, StdAddedTime;
}
public void buttonClick()
{
List<myclass> StdList = new List<myclass>();
myclass mc = new myclass();
OdbcCommand readStd =
new OdbcCommand("SELECT StdDetails, StdAddedDate," +
"StdAddedTime, StdAdderID" +
"FROM Students", Conn);
OdbcDataReader readStdreader =
readStd.ExecuteReader(CommandBehavior.SingleRow);
while (readStdreader .Read())
{
mc.StdDetails = readStdreader.GetString(0);
mc.StdDetails = readStdreader.GetString(3);
mc.StdDetails = readStdreader.GetDate(1);
mc.StdDetails = readStdreader.GetDateTime(2);
StdList.Add(mc);
}
MessageBox.Show(StdList[0].ToString());
}
在消息框中不显示值?以do.please什么帮助
//遗憾的错别字:
替代这些三线:
mc.StdDetails = readStdreader.GetString(3);
mc.StdDetails = readStdreader.GetDate(1);
mc.StdDetails = readStdreader.GetDateTime(2);
有:
mc.StdAddedDate= readStdreader.GetString(3);
mc.StdAdderID= readStdreader.GetDate(1);
mc.StdAddedTime= readStdreader.GetDateTime(2);
对于一个它看起来像你重新分配所有的数据库值只是'myclass'的StdDetails属性。 StdDetails将始终等于readStdreader.GetDateTime(2),其余属性将为null。 –