问题描述
我想从本地数据库表更新数据库的远程表。但是,当我更新的主人我是不匹配的主键fackign问题。那么,有没有我可以更新与当地相同主键远程数据库表的机制。如果我删除记录,在当地有远程主键不匹配。虽然我有两种类型的表没有主键,并与主键。
在这里,我做了这样的
如果表在本地与主键。
公共BOOL PublishDressingDetails()
{
INT isUpdated = -1;
尝试
{
数据表DressingDetailsLocalDT = cDressing.GetAllDressingDetailsDTForExport();
如果(DressingDetailsLocalDT.Rows.Count大于0)
{
// SqlDataAdapter的DA =新的SqlDataAdapter(从浇头选择*,其中客户端ID = 2,connWeb);
SqlDataAdapter的DA =新的SqlDataAdapter(SELECT * FROM DressingDetails,connWeb);
DA.InsertCommand.Transaction =交易;
DA.UpdateCommand.Transaction =交易;
DA.DeleteCommand.Transaction =交易;
SqlCommandBuilder SCB =新SqlCommandBuilder(DA);
数据集DS =新的DataSet();
DA.Fill(DS,DressingDetails);
//DS.Tables[0].PrimaryKey =新的DataColumn [] {DS.Tables [0] .Columns [客户端ID],DS.Tables [0] .Columns [ToppingID]};
DS.Tables [0] .PrimaryKey =新的DataColumn [] {DS.Tables [0] .Columns [DressingID]};
[对象] searchVal =新的对象[1];
// searchVal [0] =2;
// DataRow的drSrc;
的foreach(DataRow的drLocal在DressingDetailsLocalDT.Rows)
{
searchVal [0] = drLocal [DressingID]的ToString()。
DataRow的drSrc = DS.Tables [0] .Rows.Find(searchVal);
如果(drSrc == NULL)
{
drSrc = DS.Tables [0] .NewRow();
// drSrc [客户端ID] =2;
drSrc [DressingID] = drLocal [DressingID]的ToString()。
如果(drLocal [说明]!=的DBNull.Value)
{
。drSrc [说明] = drLocal [说明]的ToString();
}
其他drSrc [说明] =的DBNull.Value;
如果(drLocal [位置]!=的DBNull.Value)
{
。drSrc [位置] = drLocal [位置]的ToString();
}
其他drSrc [位置] =的DBNull.Value;
如果(drLocal [请将isDeleted]!=的DBNull.Value)
{
drSrc [请将isDeleted] = drLocal [请将isDeleted]的ToString()。
}
其他drSrc [请将isDeleted] =的DBNull.Value;
如果(drLocal [图标]!=的DBNull.Value)
drSrc [图标] = drLocal [图标];
其他
drSrc [图标] =的DBNull.Value;
DS.Tables [0] .Rows.Add(drSrc);
}
其他
{
// drSrc [客户端ID] =2;
drSrc [DressingID] = drLocal [DressingID]的ToString()。
如果(drLocal [说明]!=的DBNull.Value)
{
。drSrc [说明] = drLocal [说明]的ToString();
}
其他drSrc [说明] =的DBNull.Value;
如果(drLocal [位置]!=的DBNull.Value)
{
。drSrc [位置] = drLocal [位置]的ToString();
}
其他drSrc [位置] =的DBNull.Value;
如果(drLocal [请将isDeleted]!=的DBNull.Value)
{
drSrc [请将isDeleted] = drLocal [请将isDeleted]的ToString()。
}
其他drSrc [请将isDeleted] =的DBNull.Value;
如果(drLocal [图标]!=的DBNull.Value)
drSrc [图标] = drLocal [图标];
其他
drSrc [图标] =的DBNull.Value;
}
}
isUpdated = DA.Update(DS,DressingDetails);
}
}
赶上(例外前)
{
抛出前;
}
返回(isUpdated!= -1)?真假;
}
由于没有主键。
公共BOOL PublishMealDealDetail()
{
布尔isUpdated = TRUE;
尝试
{
数据表dtLocal = libEPOS.cMealDeal.GetAllMealDealDetailsDTForExport();
如果(dtLocal.Rows.Count大于0)
{
//截断表上的网页
字符串sqlTrunc =TRUNCATE TABLE MealDealDetail;
的SqlCommand CMD =新的SqlCommand(sqlTrunc,connWeb,交易);
// connWeb.Open();
cmd.ExecuteNonQuery();
SqlBulkCopy的bulkcopy =新SqlBulkCopy的(connWeb);
bulkcopy.DestinationTableName =MealDealDetail;
尝试
{
bulkcopy.WriteToServer(dtLocal);
}
赶上(例外五)
{
transaction.Rollback();
isUpdated = FALSE;
Console.WriteLine(e.Message);
}
}
}
赶上(例外前)
{
transaction.Rollback();
isUpdated = FALSE;
抛出前;
}
返回isUpdated;
}
U可以使用 System.Data.SqlClient.SqlBulkCopyOptions.KeepIdentity
,以保持身份,从一个源其他来源,同时更新表。
MSDN这里:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopyoptions(v=vs.110).aspx
System.Data.SqlClient.SqlBulkCopy bulkcopy =新System.Data.SqlClient.SqlBulkCopy(connWeb,System.Data.SqlClient.SqlBulkCopyOptions.KeepIdentity);
I want to update remote tables of a database from the local database tables.But when i update for the masters i am fackign problem of mismatch primary key.So is there a mechanism that i can update remote database tables with same primary key at local. If i delete a record at local there is mismatch of primary key at remote.Although i have both type of table with no primary key, and with primary key.
Here i did it like this
IF table at local with primary key.
public bool PublishDressingDetails()
{
int isUpdated = -1;
try
{
DataTable DressingDetailsLocalDT = cDressing.GetAllDressingDetailsDTForExport();
if (DressingDetailsLocalDT.Rows.Count > 0)
{
//SqlDataAdapter DA = new SqlDataAdapter("select * from Toppings where ClientID=2", connWeb);
SqlDataAdapter DA = new SqlDataAdapter("select * from DressingDetails", connWeb);
DA.InsertCommand.Transaction = transaction;
DA.UpdateCommand.Transaction = transaction;
DA.DeleteCommand.Transaction = transaction;
SqlCommandBuilder SCB = new SqlCommandBuilder(DA);
DataSet DS = new DataSet();
DA.Fill(DS, "DressingDetails");
//DS.Tables[0].PrimaryKey = new DataColumn[] { DS.Tables[0].Columns["ClientID"], DS.Tables[0].Columns["ToppingID"] };
DS.Tables[0].PrimaryKey = new DataColumn[] { DS.Tables[0].Columns["DressingID"] };
object[] searchVal = new object[1];
//searchVal[0] = "2";
//DataRow drSrc;
foreach (DataRow drLocal in DressingDetailsLocalDT.Rows)
{
searchVal[0] = drLocal["DressingID"].ToString();
DataRow drSrc = DS.Tables[0].Rows.Find(searchVal);
if (drSrc == null)
{
drSrc = DS.Tables[0].NewRow();
//drSrc["ClientID"] = "2";
drSrc["DressingID"] = drLocal["DressingID"].ToString();
if (drLocal["Description"] != DBNull.Value)
{
drSrc["Description"] = drLocal["Description"].ToString();
}
else drSrc["Description"] = DBNull.Value;
if (drLocal["Position"] != DBNull.Value)
{
drSrc["Position"] = drLocal["Position"].ToString();
}
else drSrc["Position"] = DBNull.Value;
if (drLocal["IsDeleted"] != DBNull.Value)
{
drSrc["IsDeleted"] = drLocal["IsDeleted"].ToString();
}
else drSrc["IsDeleted"] = DBNull.Value;
if (drLocal["icon"] != DBNull.Value)
drSrc["icon"] = drLocal["icon"];
else
drSrc["icon"] = DBNull.Value;
DS.Tables[0].Rows.Add(drSrc);
}
else
{
//drSrc["ClientID"] = "2";
drSrc["DressingID"] = drLocal["DressingID"].ToString();
if (drLocal["Description"] != DBNull.Value)
{
drSrc["Description"] = drLocal["Description"].ToString();
}
else drSrc["Description"] = DBNull.Value;
if (drLocal["Position"] != DBNull.Value)
{
drSrc["Position"] = drLocal["Position"].ToString();
}
else drSrc["Position"] = DBNull.Value;
if (drLocal["IsDeleted"] != DBNull.Value)
{
drSrc["IsDeleted"] = drLocal["IsDeleted"].ToString();
}
else drSrc["IsDeleted"] = DBNull.Value;
if (drLocal["icon"] != DBNull.Value)
drSrc["icon"] = drLocal["icon"];
else
drSrc["icon"] = DBNull.Value;
}
}
isUpdated = DA.Update(DS, "DressingDetails");
}
}
catch (Exception ex)
{
throw ex;
}
return (isUpdated != -1) ? true : false;
}
With no primary key.
public bool PublishMealDealDetail()
{
bool isUpdated = true;
try
{
DataTable dtLocal = libEPOS.cMealDeal.GetAllMealDealDetailsDTForExport();
if (dtLocal.Rows.Count > 0)
{
//truncate table on web
string sqlTrunc = "TRUNCATE TABLE MealDealDetail";
SqlCommand cmd = new SqlCommand(sqlTrunc, connWeb, transaction);
// connWeb.Open();
cmd.ExecuteNonQuery();
SqlBulkCopy bulkcopy = new SqlBulkCopy(connWeb);
bulkcopy.DestinationTableName = "MealDealDetail";
try
{
bulkcopy.WriteToServer(dtLocal);
}
catch (Exception e)
{
transaction.Rollback();
isUpdated = false;
Console.WriteLine(e.Message);
}
}
}
catch (Exception ex)
{
transaction.Rollback();
isUpdated = false;
throw ex;
}
return isUpdated;
}
U can make use of System.Data.SqlClient.SqlBulkCopyOptions.KeepIdentity
to keep identity from one source to other source while updating tables.
MSDN here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopyoptions(v=vs.110).aspx
System.Data.SqlClient.SqlBulkCopy bulkcopy = new System.Data.SqlClient.SqlBulkCopy(connWeb,System.Data.SqlClient.SqlBulkCopyOptions.KeepIdentity);
这篇关于如何更新远程表的本地数据库表相同的主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!