[英]List.Add Method is not working
public class abc
{
public abc(int ID, string Date, string jobtype, string command)
{
this.ID = ID;
this.Date = Date;
this.jobtype = jobtype;
this.command = command;
}
public int ID { get; set; }
public string Date { get; set; }
public string jobtype { get; set; }
public string command { get; set; }
}
public List<abc> getdata()
{
List<abc> data = new List<abc>();
SqlCommand cmd = new SqlCommand("SELECT * FROM [Table]",connection1);
using (connection1)
{
connection1.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
**data.Add(sdr["job_type"]);**
}
connection1.Close();
return data;
}
}
}
private DataTable Getdata(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = connection1;
try
{
connection1.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
connection1.Close();
sda.Dispose();
connection1.Dispose();
}
}
My code as above, I got an error on data.Add(sdr["job_type"]); 上面的代码,在data.Add(sdr [“ job_type”]);上出现错误。 where:
哪里:
Error 2 The best overloaded method match for 'System.Collections.Generic.List has some invalid arguments Error 3 Argument 1: cannot convert from 'object' to 'WindowsFormsApplication1.Form1.abc' 错误2'System.Collections.Generic.List的最佳重载方法匹配具有无效参数错误3参数1:无法从'对象'转换为'WindowsFormsApplication1.Form1.abc'
I try to find some solutions through Google but no luck finding one. 我尝试通过Google找到一些解决方案,但没有找到一个好运。 I quite new to C# so can anyone please tell me where I did wrong?
我是C#的新手,所以有人可以告诉我我做错了什么吗? The code are some example that I came across during searching.
该代码是我在搜索过程中遇到的一些示例。
Is there any alternative to the ways to place data from database in a list like shown above? 除了将数据库中的数据放入如上所示的列表之外,还有其他方法吗? Thanks.
谢谢。
You cannot simply cast your database object to your abc
class. 您不能简单地将数据库对象转换为
abc
类。 You will need to read each column and create a new abc
object, as your list can only contain objects abc
. 您将需要阅读每一列并创建一个新的
abc
对象,因为您的列表只能包含对象abc
。
For example (I am doing this based on MySQLReader knowledge, it may be a bit different) 例如(我是根据MySQLReader知识进行此操作的,可能有所不同)
while (sdr.Read())
{
data.Add(new abc((int)sdr["ID"],
(string)sdr["Date"],
(string)sdr["job_type"],
(string)sdr["command"]));
}
Obviously the column names need to be renamed to your exact column names. 显然,列名需要重命名为您的确切列名。
You would have to create a new abc
object from the actual row you are reading from the Database. 您将必须从数据库中读取的实际行中创建一个新的
abc
对象。
Something like 就像是
data.Add(new abc(
Convert.ToInt32(sdr["ID"]),
Convert.ToString(sdr["DATE"]),
Convert.ToString(sdr["job_type"]),
Convert.ToString(sdr["command"]))
);
May be like this 可能是这样
You need to Specify Your Properties with Creating Object of Class 您需要通过创建类对象来指定属性
data.Add(new abc() {job_type1=sdr["job_type"].ToString()});
Or 要么
Passing Data To Constructor Of Your Class Abc
将数据传递给班级
Abc
构造函数
data.Add(new abc(sdr["job_type"].ToString()));
For Reference 以供参考
using (SqlDataReader sdr = cmd.ExecuteReader())
{
abc temp = new abc();
while (sdr.Read())
{
temp = new abc();
temp.ID = Convert.ToInt32(sdr["ID"]);
temp.Date = Convert.ToDate(sdr["Date"]);
temp.jobtype = Convert.ToString(sdr["job_type"]);
temp.command = Convert.ToString(sdr["command"]);
data.Add(temp);
}
connection1.Close();
return data;
}
I suggest to modify the abc
's constructor so that it takes the SqlDataReader
as a parameter and do the assignment work inside the constructor: 我建议修改
abc
的构造函数,以使其将SqlDataReader
作为参数并在构造函数内部进行分配工作:
public class abc
{
private enum Column
{
ID = 1,
Date = 2,
jobtype = 3,
command = 4
}
public abc(SqlDataReader reader)
{
ID = sdr.GetFieldValue<int>[Column.ID];
Date = sdr.GetFieldValue<string>[Column.Date];
jobtype = sdr.GetFieldValue<string>[Column.jobtype];
command = sdr.GetFieldValue<string>[Column.command];
// or
//ID = (int)sdr["ID"];
//Date = (string)sdr["Date"];
//jobtype =(string)sdr["jobtype"];
//command = (string)sdr["command"];
}
public int ID { get; set; }
public string Date { get; set; }
public string jobtype { get; set; }
public string command { get; set; }
}
this way your loop is nice and clean and the parsing/casting (if any) encapsulated: 这样,您的循环就很干净了,并且对解析/广播 (如果有)进行了封装:
while (sdr.Read())
{
data.Add(new abc(sdr));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.