【发布时间】:2015-07-10 13:33:30
【问题描述】:
我有一个包含 5 个表的 MS Access 数据库,但我有兴趣通过 C# 控制台应用程序将两个表移动到 SQL Server:
我为 itemmaster 创建了一个连接字符串和一个DataTable,并成功地将数据从 MS Access 数据库移动到数据表中:
namespace AccessDBtoSql
{
class Program
{
static void Main(string[] args)
{
string straccessconnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= T:\Aravind\Company Master Data.accdb";
string strItemMasterSelect = "SELECT * FROM [Item Master]";
DataSet DataSet1 = new DataSet();
OleDbConnection myAccessConn = null;
try
{
myAccessConn = new OleDbConnection(straccessconnection);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}
try
{
OleDbCommand myAccessCommand = new OleDbCommand(strItemMasterSelect, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(DataSet1, "itemmaster");
DataTable itemmaster;
itemmaster = DataSet1.Tables["itemmaster"];
foreach (DataRow drcurrent in itemmaster.Rows)
{
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {18} {19} {20} {21} {22} {23} {24} {25} {26} {27} {28} {29} {30} {31} {32} {33} {34} {35} {36} {37} {38} {39} {40} {41} {42} {43} {44} {45} {46} {47} {48} {49} {50} {51} {52} {53} {54} {55} {56} {57} {58} {59} {60} {61} {62} {63} {64} {65} {66} {67} {68} {69} {70} {71} {72} {73} {74} {75} {76} {77} {78} {79} {80} {81} {82} {83} {84} {85} {86} {87} {88} {89} {90} {91} {92} {93} {94} {95} {96} {97} {98} {99} {100} {101} {102} {103} {104} {105} {106} {107} {108} {109} {110} {111} {112} {113} {114} {115} {116}",
drcurrent["Item"].ToString(),
drcurrent["Description"].ToString(),
drcurrent["Item Type"].ToString(),
drcurrent["Item Group"].ToString(),
drcurrent["Product Type"].ToString(),
drcurrent["Selection Code"].ToString(),
drcurrent["Standard Cost Price"].ToString(),
drcurrent["Inventory Unit"].ToString(),
drcurrent["Purchase Price"].ToString(),
drcurrent["Purchase Price Unit"].ToString(),
drcurrent["Latest Purchase Price"].ToString(),
drcurrent["Sales Price"].ToString(),
drcurrent["Sales Price Unit"].ToString(),
drcurrent["Phantom"].ToString(),
drcurrent["Scrap Factor"].ToString(),
drcurrent["Weight"].ToString(),
drcurrent["Signal Code"].ToString(),
drcurrent["Cost Price Component"].ToString(),
drcurrent["Last CP Trans Date"].ToString(),
drcurrent["Unit Set"].ToString(),
drcurrent["Inventory on Hand"].ToString(),
drcurrent["Purchase Unit"].ToString(),
drcurrent["Sales Unit"].ToString(),
drcurrent["Price Group"].ToString(),
drcurrent["Statistics Group"].ToString(),
drcurrent["Commission Group"].ToString(),
drcurrent["Rebate Group"].ToString(),
drcurrent["Sugg Ret Price"].ToString(),
drcurrent["Units per Carton"].ToString(),
drcurrent["Gross Weight"].ToString(),
drcurrent["Net Weight"].ToString(),
drcurrent["Freight Class"].ToString(),
drcurrent["Unit UPC"].ToString(),
drcurrent["Case UPC"].ToString(),
drcurrent["Retail/Misc only"].ToString(),
drcurrent["Additional Block"].ToString(),
drcurrent["Display Boxes"].ToString(),
drcurrent["Best Before"].ToString(),
drcurrent["Reportable"].ToString(),
drcurrent["Package (Enum)"].ToString(),
drcurrent["Package"].ToString(),
drcurrent["Style (Enum)"].ToString(),
drcurrent["Style"].ToString(),
drcurrent["Heart (Enum)"].ToString(),
drcurrent["Heart"].ToString(),
drcurrent["Basket (Enum)"].ToString(),
drcurrent["Basket"].ToString(),
drcurrent["Sugar Free (Enum)"].ToString(),
drcurrent["Sugar Free"].ToString(),
drcurrent["Flatback (Enum)"].ToString(),
drcurrent["Flatback"].ToString(),
drcurrent["Multipack (Enum)"].ToString(),
drcurrent["Multipack"].ToString(),
drcurrent["Displayer (Enum)"].ToString(),
drcurrent["Displayer"].ToString(),
drcurrent["Cross Shippable"].ToString(),
drcurrent["Freezer Item"].ToString(),
drcurrent["Novelty Flag"].ToString(),
drcurrent["Normal/Special/Multipack"].ToString(),
drcurrent["Component #1"].ToString(),
drcurrent["Component #1Units"].ToString(),
drcurrent["Component #2"].ToString(),
drcurrent["Component #2Units"].ToString(),
drcurrent["Component #3"].ToString(),
drcurrent["Component #3Units"].ToString(),
drcurrent["Component #4"].ToString(),
drcurrent["Component #4Units"].ToString(),
drcurrent["Component #5"].ToString(),
drcurrent["Component #5Units"].ToString(),
drcurrent["Component #6"].ToString(),
drcurrent["Component #6Units"].ToString(),
drcurrent["Component #7"].ToString(),
drcurrent["Component #7Units"].ToString(),
drcurrent["Component #8"].ToString(),
drcurrent["Component #8Units"].ToString(),
drcurrent["Component #9"].ToString(),
drcurrent["Component #9Units"].ToString(),
drcurrent["Component #10"].ToString(),
drcurrent["Component #10Units"].ToString(),
drcurrent["Component #11"].ToString(),
drcurrent["Component #11Units"].ToString(),
drcurrent["Component #12"].ToString(),
drcurrent["Component #12Units"].ToString(),
drcurrent["Component #13"].ToString(),
drcurrent["Component #13Units"].ToString(),
drcurrent["Component #14"].ToString(),
drcurrent["Component #14Units"].ToString(),
drcurrent["Component #15"].ToString(),
drcurrent["Component #15Units"].ToString(),
drcurrent["Component #16"].ToString(),
drcurrent["Component #16Units"].ToString(),
drcurrent["Component #17"].ToString(),
drcurrent["Component #18"].ToString(),
drcurrent["Component #18Units"].ToString(),
drcurrent["Component #19"].ToString(),
drcurrent["Component #19Units"].ToString(),
drcurrent["Component #20"].ToString(),
drcurrent["Component #20Units"].ToString(),
drcurrent["Unit Weight lbs"].ToString(),
drcurrent["Unit Weight ozs"].ToString(),
drcurrent["Unit Weight Grams"].ToString(),
drcurrent["Prepriced"].ToString(),
drcurrent["Country of Sale"].ToString(),
drcurrent["Egg"].ToString(),
drcurrent["Pallet Spec Wrapped"].ToString(),
drcurrent["Pallet Spec Non-Wrapped"].ToString(),
drcurrent["Stack Height"].ToString(),
drcurrent["Scan Unit UPC (SO/PO)"].ToString(),
drcurrent["Budget Category"].ToString(),
drcurrent["Budget Category description"].ToString(),
drcurrent["Base Item"].ToString(),
drcurrent["registered Item"].ToString(),
drcurrent["Material"].ToString(),
drcurrent["Size"].ToString(),
drcurrent["Standard"].ToString(),
drcurrent["Sales Price (CAD)"].ToString(),
drcurrent["Suggested Retail Price (CAD)"].ToString());
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
上面的查询给了我这个结果:
这是正确的,但现在我需要将所有数据从数据表移动到 SQL Server 中,我被困在这里。我正在尝试实现一个 SQL 批量更新,它开始一个事务将一次更新 10,000 条记录,如果一切顺利,如果不回滚事务,则提交。
到目前为止我所拥有的:
public void sqlbatchupdatelocal()
{
string strsqlconnection = "Data Source=KCMJF1XTR1\\SQLEXPRESS;Initial Catalog=AccessMaster1;Integrated Security=True";
using (SqlConnection con = new SqlConnection(strsqlconnection))
{
con.Open();
try
{
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
谁能帮我完成上面代码的 SQL 更新,我想从 itemmaster 数据表中取出所有 55885 条记录,但一次将 10000 条记录加载到 SQL Server 中。如果我得到这个工作,那么我也会对另一个表采用相同的方法。
感谢大家的帮助。
【问题讨论】:
标签: c# sql-server-2008 ms-access datatable sql-update