C# 如何根据一列获取数据库中的整行
我在博士后工作,我有一个问题,比如下面的问题:C# 如何根据一列获取数据库中的整行,c#,sql,database,postgresql,postgresql-9.1,C#,Sql,Database,Postgresql,Postgresql 9.1,我在博士后工作,我有一个问题,比如下面的问题: CustomerID CustomerName ContactName Address City PostalCode Country 1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany 2 Ana Trujillo Emparedad
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados Borlo 2222 México 05021 Mexico
现在,我必须得到包含城市的整行,如México
如果它是基于行数的,它是这样的(非常简单):
我知道如何以行数为基础获取行,但我不知道如何以列数据为基础获取行(在我的例子中是墨西哥)
注意请注意,我必须将每列存储在类列表中,我的意思是:
List<Coustomer> cstmr = new List<Coustomer>() ;
你想要的只是
从客户中选择*,城市='México'代码>
阅读本文了解有关使用WHERE子句的更多信息。如果我正确理解了您的问题,您希望将行的值放在列表的对象中
这可能对你有用
private IList<Coustomer> MakeCustomers(DataTable dt)
{
IList<Coustomer> list = new List<Coustomer>();
foreach (DataRow row in dt.Rows)
list.Add(MakeCustomer(row));
return list;
}
private Customer MakeCustomer(DataRow row)
{
int customerId = int.Parse(row["CustomerId"].ToString());
string CustomerName = row["CustomerName"].ToString();
string ContactName = row["ContactName"].ToString();
string country = row["Country"].ToString();
//Any other fields which might be pulling from DB
return new Customer(customerId, company, city, country);
}
私有IList MakeCustomers(数据表dt)
{
IList list=新列表();
foreach(数据行中的数据行)
list.Add(MakeCustomer(row));
退货清单;
}
私人客户MakeCustomer(数据行)
{
int customerId=int.Parse(第[“customerId”].ToString()行);
字符串CustomerName=行[“CustomerName”].ToString();
字符串ContactName=行[“ContactName”]。ToString();
字符串country=行[“country”]。ToString();
//可能从数据库中提取的任何其他字段
返回新客户(客户ID、公司、城市、国家);
}
从ColumnName=YourValue的客户中选择*,类似这样的内容?
foreach(var column in cstmr)
column.CustomerName=Ana Trujillo; // in Mexico's case
column.Country=Mexico;
private IList<Coustomer> MakeCustomers(DataTable dt)
{
IList<Coustomer> list = new List<Coustomer>();
foreach (DataRow row in dt.Rows)
list.Add(MakeCustomer(row));
return list;
}
private Customer MakeCustomer(DataRow row)
{
int customerId = int.Parse(row["CustomerId"].ToString());
string CustomerName = row["CustomerName"].ToString();
string ContactName = row["ContactName"].ToString();
string country = row["Country"].ToString();
//Any other fields which might be pulling from DB
return new Customer(customerId, company, city, country);
}