最新编辑!!!
请...我已经通过了这段代码的许多排列。我试图从数据库(SQL服务器)获取某些相关记录并导出到XML(这似乎工作!),并将它们带到该数据库的断开连接的副本,并将它们导入到该数据库,并将它们合并为主数据库key(如果密钥存在则更新,如果不存在则插入)。
数据库中有许多表,我希望所有与已更改的某个实体列表相关的表。我很早就发现,我无法使用连接执行大型复杂查询以获取我想要填充导出DataSet的所有列,因为我丢失了所有表结构。我还让Visual Studio 2005 DataSet设计器创建了一个类型化的数据集,我不确定我该怎么做,请告诉我是否应该这样做。
我正在做的是将数据导出到xml:
public void exportData(string filename, List sxOrgs) {
MyGeneratedDataSet ds = new MyGeneratedDataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
String query;
String orgList = "(";
//create the string query list of sxOrgs
foreach (String sx in sxOrgs)
{
orgList += "'" + sx + "', ";
}
orgList = orgList.Remove(orgList.Length - 2);
orgList += ")";
try
{
//tblOrganization
query = "select * from tblOrganization where tblOrganization.sxOrganization in "
+ orgList;
adapter.SelectCommand = new SqlCommand(query, Connectivity.Connection());
adapter.Fill(ds.tblOrganization);
//tblCategory
query = "select * from tblCategory where sxCategory in " +
"(select sxCategory from lnkOrganizationCategory " +
"where lnkOrganizationCategory.sxOrganization in " +
orgList + ")";
adapter.SelectCommand = new SqlCommand(query, Connectivity.Connection());
adapter.Fill(ds.tblCategory);
//lnkOrganizationCategory
query = "select * from lnkOrganizationCategory where sxOrganization in " + orgList;
adapter.SelectCommand = new SqlCommand(query, Connectivity.Connection());
adapter.Fill(ds.lnkOrganizationCategory);
//tblContact
query = "select * from tblContact where sxContact in " +
"(select sxContact from lnkOrganizationContact " +
"where lnkOrganizationContact.sxOrganization in " +
orgList + ")";
adapter.SelectCommand = new SqlCommand(query, Connectivity.Connection());
adapter.Fill(ds.tblContact);
//lnkOrganizationContact
query = "select * from lnkOrganizationContact where lnkOrganizationContact.sxOrganization in " + orgList;
adapter.SelectCommand = new SqlCommand(query, Connectivity.Connection());
adapter.Fill(ds.lnkOrganizationContact);
} catch(Exception ex) {
Debug.WriteLine(ex.Message);
}
ds.WriteXml(filename, XmlWriteMode.IgnoreSchema);
}
正如你所看到的,我一遍又一遍地重复使用相同的TableAdapeter,最后编写的XML看起来很好看,每个表都有一个标签,每个表中都有一个标签,正是我想要的,并能够将其合并回来。这是导入代码:
//reads data from an xml file and merges it into existing db
public static void ImportData(string data)
{
try
{
MyGeneratedDataSet newData = new MyGeneratedDataSet();
StreamWriter sw = new StreamWriter("newdata.xml", false);
Debug.WriteLine(data);
sw.Write(data);
sw.Close();
XmlTextReader reader = new XmlTextReader(new MemoryStream(ASCIIEncoding.Default.GetBytes(data)));
newData.ReadXml("newData.xml");
MyGeneratedDataSet currentData = new MyGeneratedDataSet();
//tblOrganization
SqlDataAdapter tblOrganizationDataAdapter = new SqlDataAdapter("select * from tblOrganization", Connectivity.Connection());
SqlCommandBuilder tblOrganizationCommandBuilder = new SqlCommandBuilder(tblOrganizationDataAdapter);
tblOrganizationDataAdapter.Fill(currentData, "tblOrganization");
//tblContact
SqlDataAdapter tblContactDataAdapter = new SqlDataAdapter("select * from tblContact", Connectivity.Connection());
SqlCommandBuilder tblContactCommandBuilder = new SqlCommandBuilder(tblContactDataAdapter);
tblContactDataAdapter.Fill(currentData, "tblContact");
//tblCategory
SqlDataAdapter tblCategoryDataAdapter = new SqlDataAdapter("select * from tblCategory", Connectivity.Connection());
SqlCommandBuilder tblCategoryCommandBuilder = new SqlCommandBuilder(tblCategoryDataAdapter);
tblCategoryDataAdapter.Fill(currentData, "tblCategory");
//lnkOrganizationCategory
SqlDataAdapter lnkOrganizationCategoryDataAdapter = new SqlDataAdapter("select * from lnkOrganizationCategory", Connectivity.Connection());
SqlCommandBuilder lnkOrganizationCategoryCommandBuilder = new SqlCommandBuilder(lnkOrganizationCategoryDataAdapter);
lnkOrganizationCategoryDataAdapter.Fill(currentData, "lnkOrganizationCategory");
//lnkOrganizationContact
SqlDataAdapter lnkOrganizationContactDataAdapter = new SqlDataAdapter("select * from lnkOrganizationContact", Connectivity.Connection());
SqlCommandBuilder lnkOrganizationContactCommandBuilder = new SqlCommandBuilder(lnkOrganizationContactDataAdapter);
lnkOrganizationContactDataAdapter.Fill(currentData, "lnkOrganizationContact");
Debug.WriteLine(tblOrganizationDataAdapter.SelectCommand.CommandText);
Debug.WriteLine(tblOrganizationDataAdapter.UpdateCommand.CommandText);
currentData.Merge(newData);
tblOrganizationDataAdapter.Update(currentData);
tblContactDataAdapter.Update(currentData);
tblCategoryDataAdapter.Update(currentData);
lnkOrganizationCategoryDataAdapter.Update(currentData);
lnkOrganizationContactDataAdapter.Update(currentData);
} catch (Exception ex) {
Debug.WriteLine(ex.Message);
}
}
目前,导入函数末尾附近的Debug.WriteLine显示tblOrganizationTableAdapter的UpdateCommand为null。视觉设计师告诉我,它创造了这个,但是如果我所要做的就是创造它,当然,我会这样做,但我现在已经重写了这么多次了(而且还有更多的桌子) ,我仍然不明白发生了什么。我该怎么做?!
非常感谢你! 约书亚
此代码几乎可以使用。它现在不再失败了,但是,我最终得到了所有重复的行,而不是通过主键比较进行更新!
答案 0 :(得分:2)
你在做什么听起来是对的。我不确定你的确切问题是什么,但这是流程的细分:
执行步骤#4时,表适配器会查看DataSet
中的每个表,并找出哪些行已更改。它知道,因为每个DataRow
中的每个DataTable
都会将DataRowState
设置为Added
,Modified
或Deleted
。 Merge()
。然后,表适配器在底层数据库中执行请求的更改,并将DataRowState
设置为Unchanged
DataRow
(当然,Deleted
除外)。当此过程完成时,底层数据库应该包含NewData数据集包含的所有更改。
答案 1 :(得分:1)
布拉赫:
代码:
using (SqlDataAdapter dap = new SqlDataAdapater(myConnection, "SELECT * FROM MyTable"))
dap.Update(myDataTable)
答案 2 :(得分:0)
更新:
adapter.Update(currentData, "table1")
一般来说,它可能更容易直接在sql中合并2个表,但你的应用可能需要处理xml文件..
使用合并/更新的示例:
http://msdn.microsoft.com/en-us/library/aa325628%28VS.71%29.aspx
在t-sql中,您可以使用MERGE:
在一个语句中执行此操作http://technet.microsoft.com/en-us/library/bb510625.aspx
调整它以适应您想要更改/更新行的条件