数据库A,B结构完全相同。A中的表T1,B中的表T1结构完全相同。两个表之间的更新。
方法一:用sql存储进程,可以实现,这个没问题。
方法二:想用dataset方式更相互更新,怎么实现?在网上看了一堆关于dataset更新的贴子,但没有解决问题。
SqlDataAdapter daTA = new SqlDataAdapter("select * from t1", A);
SqlCommandBuilder cbTa = new SqlCommandBuilder(daTA);
DataSet dsA = new DataSet();
daTa.Fill(dsA,"t1"); SqlDataAdapter daTB = new SqlDataAdapter("select * from t1", B);
SqlCommandBuilder cbTB = new SqlCommandBuilder(daTB);
DataSet dsA = new DataSet();
daTB.Fill(dsB,"t1"); 以下的代码怎么写,请大家帮忙?-------------------------------------
另:方法一和方法二哪种效率更高?
方法一:用sql存储进程,可以实现,这个没问题。
方法二:想用dataset方式更相互更新,怎么实现?在网上看了一堆关于dataset更新的贴子,但没有解决问题。
SqlDataAdapter daTA = new SqlDataAdapter("select * from t1", A);
SqlCommandBuilder cbTa = new SqlCommandBuilder(daTA);
DataSet dsA = new DataSet();
daTa.Fill(dsA,"t1"); SqlDataAdapter daTB = new SqlDataAdapter("select * from t1", B);
SqlCommandBuilder cbTB = new SqlCommandBuilder(daTB);
DataSet dsA = new DataSet();
daTB.Fill(dsB,"t1"); 以下的代码怎么写,请大家帮忙?-------------------------------------
另:方法一和方法二哪种效率更高?
解决方案 »
- 求解
- 关于C#反射的疑问,传入方法的对象的属性值会被更改?
- 怎么过滤掉一个字符串除了汉字字母以后的其他任何字符?
- 不在同一服务器上 如何 读取exchange server 上的邮件
- 如何用C#制作xp窗体风格的界面 急急!!
- NSIS软件打包问题?WINFORM程序打包时我想附加第三方的软件,请问大家怎么用NSIS打包时怎么再添加两个其它EXE程序安装时让它自动全部安装
- 求treeview的下载,最好是国内的下载地址(英语水平有限)
- newredhat进来给你分
- 难题!如何实现TREEVIEW中节点的移动和合并?(WEB)
- 高手!如何用C#调用VB中生成的ActiveX DLL?或其它的COM,能举简单例子讲讲吗?
- .NET虚拟技术。
- 关于从两个float型数数组中提取相同的数
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
int iRows= dsTB.Tables[0].Rows.Count;//数据也已经传到了dsTB里
daTB.Update(dsTB,"t1");
dsTB.AcceptChanges();
//但daTB中并没有数据,B库中的表t1也无数据。
{
static void Main()
{
string connectionString = GetConnectionString();
// Open a sourceConnection to the AdventureWorks database.
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open(); // Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
sourceConnection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart); // Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new SqlCommand(
"SELECT ProductID, Name, " +
"ProductNumber " +
"FROM Production.Product;", sourceConnection);
SqlDataReader reader =
commandSourceData.ExecuteReader(); // Set up the bulk copy object using a connection string.
// In the real world you would not use SqlBulkCopy to move
// data from one table to the other in the same database.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(connectionString))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns"; try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Close the SqlDataReader. The SqlBulkCopy
// object is automatically closed at the end
// of the using block.
reader.Close();
}
} // Perform a final count on the destination
// table to see how many rows were added.
long countEnd = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Ending row count = {0}", countEnd);
Console.WriteLine("{0} rows were added.", countEnd - countStart);
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
} private static string GetConnectionString()
// To avoid storing the sourceConnection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}
(
c1,
c2,
c3
)
SELECT c1,c2,c3 FROM t1
我明白效率肯定是用进程写高过用dataset。
但希望能搞明白用dataset怎么成功实现呢?
用dataSet的话,你需要比较这两个表的数据,如果存在就要比较是不是一样,如果一样就什么都不做,如果不一样,就把不一样的行的状态改为跟新,并把数据修改,如果不存在就生成一个新行加进去,这样你调用Update方法才可以更新数据库里的数据,它是根据DataSet的表里的行的状态执行相应操作的。你这样做他什么操作都不做,数据不变的。
用DataSet太麻烦了。