C# } } 如果(origID==-1) { int insertData=Convert.ToInt32(askta.InsertActiveServiceKeys(类别、代理、路线、方向、服务键、语言、活动服务键)); } 其他的 { int update
} } 如果(origID==-1) { int insertData=Convert.ToInt32(askta.InsertActiveServiceKeys(类别、代理、路线、方向、服务键、语言、活动服务键)); } 其他的 { int updateData=Convert.ToInt32(askta.UpdateActiveServiceKeys(类别、机构、路线、方向、服务键、语言、活动服务键、origID)); } i++; }C# } } 如果(origID==-1) { int insertData=Convert.ToInt32(askta.InsertActiveServiceKeys(类别、代理、路线、方向、服务键、语言、活动服务键)); } 其他的 { int update,c#,asp.net,C#,Asp.net,} } 如果(origID==-1) { int insertData=Convert.ToInt32(askta.InsertActiveServiceKeys(类别、代理、路线、方向、服务键、语言、活动服务键)); } 其他的 { int updateData=Convert.ToInt32(askta.UpdateActiveServiceKeys(类别、机构、路线、方向、服务键、语言、活动服务键、origID)); } i++; } 您在那里发布了很多代码,但您的问题并不十分清楚。你说你
您在那里发布了很多代码,但您的问题并不十分清楚。你说你“抓不到原始ID”-你到底是什么意思?额外的方括号是打字错误吗<代码>GetActiveServiceKeys.Rows[[“ActiveServiceKeyId”]不清楚您为什么需要原始ID。代码正在更新或插入。同一组数据是否有多行,您需要“记住”或查找现有ID?如果有现有数据,我希望运行更新而不是插入。我的问题是,我无法获取每行的每个ID。方括号et输入错误是故意的。我不知道如何获取原始id来运行更新。嗯……我对LINQ不太熟悉。这是拖放代码吗?@Bry4n:当然,只需要将自己创建为新类MyDTO。还要确保2
askta
方法实际返回一个整数。您必须确保GetActiveServiceKeys。行
位是正确的。@p.campbelldt
需要更改为temproutedatable
不是吗?@p.campbell我在originalID
上得到一个错误,它说它需要int
在foreach
中@Bry4n:对,dt
是我在测试中的本地代码。re:originalID
。。。确保它在DTO中定义为字符串,并且GetActiveServiceKeys.Rows
返回字符串。请参阅我更新的pastebin链接。
protected void SubmitCSV_Click(object sender, ImageClickEventArgs e)
{ //Check Routes File Uploader for file
if (ImportCSV.HasFile)
{
//Get File name
string fileName = ImportCSV.FileName;
//Read CSV
StreamReader ReadUploadedFile = new StreamReader(ImportCSV.FileContent);
//Parse CSV
var pathOfCSVFile = ReadUploadedFile;
//Gather CSV contents
var adapter = new GenericParsing.GenericParserAdapter(pathOfCSVFile);
//Ignore first row
adapter.FirstRowHasHeader = true;
DataTable TempRouteDataTable = adapter.GetDataTable();
if (ExcelDDL.SelectedValue == "Active Service Keys" && fileName == "ActiveServiceKeys.csv")
{
SEPTA_DS.ActiveServiceKeysTBLDataTable GetActiveServiceKeys = (SEPTA_DS.ActiveServiceKeysTBLDataTable)askta.GetData();
var hasData = GetActiveServiceKeys.Rows.Count > 0;
//Loop through each row and insert into database
foreach (DataRow row in TempRouteDataTable.Rows)
{
//Gather column headers
var category = Convert.ToString(CategoryDDL.SelectedItem);
var agency = Convert.ToString(row["Agency"]);
var route = Convert.ToString(row["Route"]);
var direction = Convert.ToString(row["Direction"]);
var serviceKey = Convert.ToString(row["Service Key"]);
var language = Convert.ToString(row["Language"]);
var activeServiceKeys = Convert.ToString(row["Active Service Keys"]);
//Check if data already exists
if (hasData == true)
{
var originalID = Convert.ToInt32(GetActiveServiceKeys.Rows[0] ["ActiveServiceKeysID"]);
int updateData = Convert.ToInt32(askta.UpdateActiveServiceKeys(category, agency, route, direction, serviceKey, language, activeServiceKeys, originalID));
}
else
{
int insertData = Convert.ToInt32(askta.InsertActiveServiceKeys(category, agency, route, direction, serviceKey, language, activeServiceKeys));
}
}
}
}
}
foreach ( var item in dt.Rows.Cast<DataRow>().Select(r=> new MyDTO {
category = CategoryDDL.SelectedItem.ToString(),
agency = r["Agency"].ToString(),
route = r["Route"].ToString(),
direction = r["Direction"].ToString(),
serviceKey= r["Service Key"].ToString(),
language = r["Language"].ToString(),
originalID = GetActiveServiceKeys.Rows[r["Active Service Keys"].ToString()]
activeServiceKeys = r["Active Service Keys"].ToString()}
))
{
if (!string.IsNullOrEmpty(item.originalID))
int updateData = askta.UpdateActiveServiceKeys(item);
else
int insertData = askta.InsertActiveServiceKeys(item);
}
//Loop through each row and insert into database
int i = 0;
foreach (DataRow row in TempRouteDataTable.Rows)
{
//Gather column headers
var category = Convert.ToString(CategoryDDL.SelectedItem);
var agency = Convert.ToString(row["Agency"]);
var route = Convert.ToString(row["Route"]);
var direction = Convert.ToString(row["Direction"]);
var serviceKey = Convert.ToString(row["Service Key"]);
var language = Convert.ToString(row["Language"]);
var activeServiceKeys = Convert.ToString(row["Active Service Keys"]);
var origID = -1;
if (GetActiveServiceKeys.Rows.Count > 0)
{
origID = Convert.ToInt32(GetActiveServiceKeys.Rows[i]["ActiveServiceKeysID"]);
var GetID = (SEPTA_DS.ActiveServiceKeysTBLDataTable)askta.GetDataByID(origID);
if (GetID.Rows.Count < 1)
{
origID = -1;
}
}
if (origID == -1)
{
int insertData = Convert.ToInt32(askta.InsertActiveServiceKeys(category, agency, route, direction, serviceKey, language, activeServiceKeys));
}
else
{
int updateData = Convert.ToInt32(askta.UpdateActiveServiceKeys(category, agency, route, direction, serviceKey, language, activeServiceKeys, origID));
}
i++;
}