我正在尝试将.DBF文件中的表导入MSSQL数据库... 我的代码适用于400行,但如果有更多,则SaveChanges根本不更新数据库。
我的代码的作用:
打开OleDb连接,通过SelectCommand将数据读入DataTable 然后我开始“for循环”,将行放入实体模型对象
循环完成时我SaveChanges - 但它不会影响MSSQL db
Visual Studio没有显示错误或异常?!它适用于400行以下的.DBF文件,但我有一个1200行的文件,我想保存到DB请帮忙!
DataTable dt = new DataTable();
try
{
// define the connections to the .dbf file
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Path + ";Extended Properties=dBase III");
OleDbCommand command = new OleDbCommand("SELECT sif_komin, naziv, oib, mjesto, adresa, telefon, telefax, mobitel, kontakt from KUPCI.dbf", connection);
//open the connection and read in all the data from .dbf file into a datatable
connection.Open();
dt.Load(command.ExecuteReader());
connection.Close();
}
catch(OleDbException)
{
//handle exception
}
using (Entities db = new Entities()) //Entity object
{
try
{
db.Komitents.RemoveRange(db.Komitents);
for (int i = 0; i < 400; i++)
{
DataRow row = dt.Rows[i];
if (row["naziv"].ToString() != "")// skips header row
{
Komitent k = new Komitent();
k.Sif_komit = Convert.ToInt32(row["sif_komin"].ToString());
k.Naziv = row["naziv"].ToString();
k.Oib = row["oib"].ToString();
k.Mjesto = row["mjesto"].ToString();
k.Adresa = row["adresa"].ToString();
k.Telefon = row["telefon"].ToString();
k.Telefax = row["telefax"].ToString();
k.Mobitel = row["mobitel"].ToString();
k.Kontakt = row["kontakt"].ToString();
db.Komitents.Add(k);
bw.ReportProgress((int)(100.0 / dt.Rows.Count) * i, null);
if (pb.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
{
pb.Value = (int)((100.0 / dt.Rows.Count) * i);
});
}
}
}
db.SaveChanges();
答案 0 :(得分:0)
我怀疑这个问题与Entity Framework真的有关。保存400件物品甚至100,000件物品应该没有任何区别!
我已经更新了你的代码
检查AddRange中真正发生的事情,列表中是否包含1200项或0项?
using (Entities db = new Entities()) //Entity object
{
try
{
db.Komitents.RemoveRange(db.Komitents);
// Jonathan: Let make 2 SaveChanges call for now to understand what's happening
db.SaveChanges();
// Jonathan: Create a list to use AddRange to improve performance
List<Komitent> list = new List<Komitent>();
for (int i = 0; i < 400; i++)
{
DataRow row = dt.Rows[i];
if (row["naziv"].ToString() != "")// skips header row
{
Komitent k = new Komitent();
k.Sif_komit = Convert.ToInt32(row["sif_komin"].ToString());
k.Naziv = row["naziv"].ToString();
k.Oib = row["oib"].ToString();
k.Mjesto = row["mjesto"].ToString();
k.Adresa = row["adresa"].ToString();
k.Telefon = row["telefon"].ToString();
k.Telefax = row["telefax"].ToString();
k.Mobitel = row["mobitel"].ToString();
k.Kontakt = row["kontakt"].ToString();
list.Add(k);
bw.ReportProgress((int)(100.0 / dt.Rows.Count) * i, null);
if (pb.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
{
pb.Value = (int)((100.0 / dt.Rows.Count) * i);
});
}
}
}
// Jonathan: Put a break point here to make sure the list contains 1200 rows
db.AddRange(list);
db.SaveChanges();
}
catch
{
// Jonathan: Let throw the error for now until it fixed
throw;
}
}
修改:添加信息以了解正在发生的事情
技巧1:使用SQL事件探查器查看insert命令是否至少发送到SQL Server。
提示2:尝试查找有潜在错误的行,尝试添加500,550,552等...直到找到正确的行数开始导致问题。查看最后一行,看看是否一切正常或用自定义值替换它。
提示3:确保在两个catch中都有一个断点,以防函数调用者也使用try / catch并且不处理错误。