先上出错信息:
经常是前十几条数据可以正常插入 后边的就会出错,望有高手指点一下
System.AccessViolationException: Attempted to read or write protected memory. Th
is is often an indication that other memory is corrupt.
at Microsoft.Navision.CFront.NativeMethods.ModifyRecSQL(Int32 tableHandle, In
t32 recordHandle)
at Microsoft.Navision.CFront.CFrontDotNet.ModifyRecord(Int32 tableHandle, Int
32 recordHandle)
at ItemClient.Program.MisItem2NAV(DataTable Dt, String RegionCode) in C:\Docu
ments and Settings\Administrator\My Documents\Visual Studio 2005\Projects\ItemCl
ient\ItemClient\Program.cs:line 278
at ItemClient.Program.Main(String[] args) in C:\Documents and Settings\Admini
strator\My Documents\Visual Studio 2005\Projects\ItemClient\ItemClient\Program.c
s:line 70
方法如下:
显示初始化Global.cf2的代码:
Global.cf2 = new CFrontDotNet(NavisionDriverType.Sql, "C:\\Program Files\\Microsoft Business Solutions-Navision\\Client");
String strServer, strUserName, strDatabase, strPWD;
strServer = "192.168.2.250";
NavisionNetType strNetType = NavisionNetType.SqlDefault;
strDatabase = "NAVADLINK";
strUserName = "nav"; //DB login name
strPWD = "navision";
Global.cf2.ConnectServerAndOpenDatabase(strServer, strNetType, strDatabase,2000, false, false, strUserName, strPWD);
Global.cf2.OpenCompany(ConfigurationManager.AppSettings["CompanyName"]);
protected static void MisItem2NAV(System.Data.DataTable Dt,String RegionCode)
{
int hTable, hRec;
hTable = Global.cf2.OpenTable(Global.cf2.TableNo("Item"));
hRec = Global.cf2.AllocRecord(hTable);
Global.cf2.BeginWriteTransaction();
foreach (DataRow myReader in Dt.Rows)
{
string RegionCodeTemp = RegionCode;
string NO = myReader["No"].ToString().Trim();
bool isNew = true;
string Measure = myReader["Base Unit of Measure"].ToString().Trim();
String _SQL1 = "select count(No_) from [" + ConfigurationManager.AppSettings["CompanyName"] + "$Item] where [No_]='" + NO + "' AND [Region Code]='" + RegionCode + "'";
if (SqlHelp.SqlExcuteScalar(_SQL1, NAVCONN).ToString() != "0")
{
isNew = false;
}
else
{
_SQL1 = "select count(No_) from [" + ConfigurationManager.AppSettings["CompanyName"] + "$Item] where [No_]='" + NO + "'";
if (SqlHelp.SqlExcuteScalar(_SQL1, NAVCONN).ToString() != "0")
{
isNew = false;
RegionCodeTemp = "ALL";
}
}
Global.cf2.InitRecord(hTable, hRec);
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "No."), NO);
//[No_ 2]
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Description"), myReader["Description 1"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Search Description"), myReader["Description 1"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Description 2"), myReader["Description 2"].ToString());
//[Class]
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Base Unit of Measure"), Measure);
//[Price Unit Conversion]
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Inventory Posting Group"), myReader["Inventory Posting Group"].ToString());
//[Shelf No_]
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Unit Price"), myReader["Unit Price"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Standard Cost"), myReader["Standard Cost"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Cost is Adjusted"), myReader["Cost is Adjusted"].ToString());
//[Allow Online Adjustment]
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Vendor No."), myReader["Vendor No_"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Unit List Price"), myReader["Unit List Price"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Net Weight"), myReader["Net Weight"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Last Date Modified"), DateTime.Now.ToString("yyyy/MM/dd"));
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Price Includes VAT"), myReader["Price Includes VAT"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Gen. Prod. Posting Group"), myReader["Gen_ Prod_ Posting Group"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Country of Origin Code"), myReader["Country of Origin Code"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "VAT Prod. Posting Group"), myReader["VAT Prod_ Posting Group"].ToString());
//[Reserve]
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Low-Level Code"), myReader["Low-Level Code"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Lot Size"), myReader["Lot Size"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Minimum Order Quantity"), myReader["Minimum Order Quantity"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Order Multiple"), myReader["Order Multiple"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Sales Unit of Measure"), myReader["Sales Unit of Measure"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Purch. Unit of Measure"), myReader["Purch_ Unit of Measure"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Item Category Code"), myReader["Item Category Code"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Product Group Code"), myReader["Product Group Code"].ToString());
Global.cf2.StringToField(hTable, hRec, Global.cf2.FieldNo(hTable, "Region Code"), RegionCodeTemp);
if (isNew)
{
Global.cf2.InsertRecord(hTable, hRec);
Console.WriteLine("Insert {0} item: {1}", RegionCode, NO);
}
else
{
Global.cf2.ModifyRecord(hTable, hRec);
Console.WriteLine("Modify {0} item: {1}", RegionCode, NO);
}
}
Global.cf2.EndWriteTransaction();
Global.cf2.FreeRecord(hRec);
Global.cf2.CloseTable(hTable);
}
看一下你的第 line 70 这须代码。。