未添加到数据集新列的值
本文关键字:新列 数据集 添加 | 更新日期: 2023-09-27 17:53:44
我为我的datatable
添加了一列,并根据条件向该列添加值,但该值未添加到datatable
(即使应该有值)
这是我使用的
代码的一部分conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;
Data Source =" + Server.MapPath("App_Data''LR Product Database 2000.mdb"));
conn.Open();
Dictionary<string, string> items = new Dictionary<string, string>();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT CODE, TITLE FROM tblProducts";
OleDbDataReader dbread = cmd.ExecuteReader();
while (dbread.Read())
{
productCode = (string)dbread["CODE"];
productTitle = (string)dbread["TITLE"];
System.Diagnostics.Debug.Write(productCode + " ");
items.Add(productCode, productTitle);
}
sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["LRVWebsite"].ToString());
sqlCon.Open();
dsSql = new DataSet();
SqlDataAdapter dba = new SqlDataAdapter(@"SELECT C.CustomerFirstName,C.CustomerLastName, C.CustomerCompany,C.CustomerPosition,C.CustomerCountry,C.CustomerProvince,C.CustomerContact,CP.ActionDate,CP.ProductCode,CP.CustomerEmail FROM tblCustomers C INNER JOIN tblCustomerProducts CP ON C.CustomerEmail = CP.CustomerEmail ORDER BY ActionDate DESC", connString);
dba.Fill(dsSql,"Products");
DataTable dt = dsSql.Tables["Products"];
dt.Columns.Add("Title", typeof(string));
foreach (DataRow dr in dt.Rows)
{
if(items.ContainsKey(dr["ProductCode"].ToString()))
{
dr["Title"] = items[dr["ProductCode"].ToString()];
}
}
如何根据条件将值添加到新列
您可以缩短第一部分(+使用using
)并使用Enumerable.Join
:
DataTable tblAccess = new DataTable();
using(var con = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source =" + Server.MapPath("App_Data''LR Product Database 2000.mdb"))
using(var da = new OleDbDataAdapter("SELECT CODE, TITLE FROM tblProducts", con))
{
da.Fill(tblAccess);
}
DataTable tblSqlServer = new DataTable();
using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["LRVWebsite"].ToString()))
using(var da = new SqlDataAdapter("SELECT C.CustomerFirstName,C.CustomerLastName, C.CustomerCompany,C.CustomerPosition,C.CustomerCountry,C.CustomerProvince,C.CustomerContact,CP.ActionDate,CP.ProductCode,CP.CustomerEmail FROM tblCustomers C INNER JOIN tblCustomerProducts CP ON C.CustomerEmail = CP.CustomerEmail ORDER BY ActionDate DESC", con))
{
da.Fill(tblSqlServer);
}
现在很容易用Linq-To-DataSet
连接两个表,并获得sql-server行的新标题:
var both = from rowSql in tblSqlServer.AsEnumerable()
join rowAcc in tblAccess.AsEnumerable()
on rowSql.Field<string>("ProductCode") equals rowAcc.Field<string>("ProductCode")
select new { rowSql = rowSql, newTitle = rowAcc.Field<string>("Title") };
foreach (var x in both)
{
x.rowSql.SetField("Title", x.newTitle);
}
您可以这样做,在select '' AS Title
中创建一个空列,然后填充它。您还可以将逻辑放入sql中(连接到items表):
SqlDataAdapter dba = new SqlDataAdapter(@"SELECT '' as Title, C.CustomerFirstName,C.CustomerLastName, C.CustomerCompany,C.CustomerPosition,C.CustomerCountry,C.CustomerProvince,C.CustomerContact,CP.ActionDate,CP.ProductCode,CP.CustomerEmail FROM tblCustomers C INNER JOIN tblCustomerProducts CP ON C.CustomerEmail = CP.CustomerEmail ORDER BY ActionDate DESC", connString);
dba.Fill(dsSql,"Products");
DataTable dt = dsSql.Tables["Products"];
foreach (DataRow dr in dt.Rows)
{
if(items.ContainsKey(dr["ProductCode"].ToString()))
{
dr["Title"] = items[dr["ProductCode"].ToString()];
}
}