外部调用 AX 的表方法 类方法
外部使用存储过程,或是SQL语句最多只能进行查询和删除的动作,不能增加和修改。
因为AX,每一行都有一个Recid字段,是系统生成的,为了标识系统唯一行数据。
通过BC连接来访问AX,调用AX的类,调用Job,使用AX的表,进行增,删,改,查。使用表的方法,Display等。
引用Microsoft.Dynamics.BusinessConnectorNet,使用域票据,来访问AX。
这新的一年共享一个,我瞎写的AXHelper,愿与君共勉。
/*
***********************************************
*
* Copyright(c) Kurodo
*
* Copyright(c) Kurodo
*
* CLR : 3.5
*
* FileName : AXHelper.cs
*
* Author : Kurodo
*
* CreatTime : 2011/11/29 15:28:43
*
************************************************ */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using Microsoft.Dynamics.BusinessConnectorNet;
namespace AXLibrary
{
public class AXHelper
{
///
/// Create Method
///
/// 表名
/// 字段值字典
public static Boolean Create(String tableName, IDictionary dict)
{
Boolean ret = false;
Axapta ax = new Axapta();
NetworkCredential nc = new NetworkCredential( " kurodo ", " abc@123 ", "kurodo .cn ");
try
{
ax.LogonAs( "kurodo ", "kurodo ", nc, null, null, null, null);
IEnumerator> dem = dict.GetEnumerator();
using (AxaptaRecord axRecord = ax.CreateAxaptaRecord(tableName))
{
ax.TTSBegin();
axRecord.Clear();
axRecord.InitValue();
while (dem.MoveNext())
{
String key = dem.Current.Key;
Object value = dem.Current.Value;
axRecord.set_Field(key,value);
}
if (axRecord.ValidateWrite())
{
axRecord.Insert();
ax.TTSCommit();
ret = true;
}
else
{
ax.CallStaticClassMethod( " CAMErrorInfo ", " throwInfo ");
ax.TTSAbort();
}
}
ax.Logoff();
}
catch (Exception ex)
{
ax.Logoff();
MessageBox.Show(ex.Message);
// throw new Exception(ex.Message);
}
return ret;
}
///
/// Update Method
///
/// 表名
/// 键名称
/// 条件值
/// 字段值字典
public static Boolean Update(String tableName,String pkey,String where,IDictionary dict)
{
return Update(tableName, pkey, where, null, null, dict);
}
///
/// Update Method
///
/// 表名
/// 键名称
/// 条件值
/// 字段值字典
public static Boolean Update(String tableName, String pkey1, String where1, String pkey2, String where2, IDictionary dict)
{
return Update(tableName, pkey1, where1, pkey2, where2, null, null, dict);
}
///
/// Update Method
///
/// 表名
/// 键名称
/// 条件值
/// 字段值字典
public static Boolean Update(String tableName, String pkey1, String where1,String pkey2,String where2,String pkey3,String where3,IDictionary dict)
{
Boolean ret = false;
Axapta ax = new Axapta();
NetworkCredential nc = new NetworkCredential( "kurodo ", " abc@123 ", "kurodo .cn ");
try
{
ax.LogonAs( "kurodo ", "kurodo ", nc, null, null, null, null);
IEnumerator> dem = dict.GetEnumerator();
using (AxaptaRecord axRecord = ax.CreateAxaptaRecord(tableName))
{
String sql = String.Empty;
ax.TTSBegin();
if (pkey3 != null)
{
sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' && %1.{2} == {3} && %1.{4} == {5} ", pkey1, where1,pkey2,where2,pkey3,where3);
}
else if (pkey2 != null)
{
sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' && %1.{2} == {3} ", pkey1, where1, pkey2, where2);
}
else
{
sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' ", pkey1, where1);
}
axRecord.ExecuteStmt(sql);
if (axRecord.Found)
{
while (dem.MoveNext())
{
string key = dem.Current.Key;
Object value = dem.Current.Value;
axRecord.set_Field(key, value);
}
// if (axRecord.ValidateWrite())
// {
axRecord.Update();
ax.TTSCommit();
ret = true;
// }
}
else
ax.TTSAbort();
}
ax.Logoff();
}
catch (Exception ex)
{
ax.Logoff();
throw new Exception(ex.Message);
}
return ret;
}
///
/// Select Method
///
/// 表名称
/// 键名称
/// 条件值
/// 字段列表
///
public static IList
* CLR : 3.5
*
* FileName : AXHelper.cs
*
* Author : Kurodo
*
* CreatTime : 2011/11/29 15:28:43
*
************************************************ */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using Microsoft.Dynamics.BusinessConnectorNet;
namespace AXLibrary
{
public class AXHelper
{
///
/// Create Method
///
/// 表名
/// 字段值字典
public static Boolean Create(String tableName, IDictionary
{
Boolean ret = false;
Axapta ax = new Axapta();
NetworkCredential nc = new NetworkCredential( " kurodo ", " abc@123 ", "kurodo .cn ");
try
{
ax.LogonAs( "kurodo ", "kurodo ", nc, null, null, null, null);
IEnumerator
using (AxaptaRecord axRecord = ax.CreateAxaptaRecord(tableName))
{
ax.TTSBegin();
axRecord.Clear();
axRecord.InitValue();
while (dem.MoveNext())
{
String key = dem.Current.Key;
Object value = dem.Current.Value;
axRecord.set_Field(key,value);
}
if (axRecord.ValidateWrite())
{
axRecord.Insert();
ax.TTSCommit();
ret = true;
}
else
{
ax.CallStaticClassMethod( " CAMErrorInfo ", " throwInfo ");
ax.TTSAbort();
}
}
ax.Logoff();
}
catch (Exception ex)
{
ax.Logoff();
MessageBox.Show(ex.Message);
// throw new Exception(ex.Message);
}
return ret;
}
///
/// Update Method
///
/// 表名
/// 键名称
/// 条件值
/// 字段值字典
public static Boolean Update(String tableName,String pkey,String where,IDictionary
{
return Update(tableName, pkey, where, null, null, dict);
}
///
/// Update Method
///
/// 表名
/// 键名称
/// 条件值
/// 字段值字典
public static Boolean Update(String tableName, String pkey1, String where1, String pkey2, String where2, IDictionary
{
return Update(tableName, pkey1, where1, pkey2, where2, null, null, dict);
}
///
/// Update Method
///
/// 表名
/// 键名称
/// 条件值
/// 字段值字典
public static Boolean Update(String tableName, String pkey1, String where1,String pkey2,String where2,String pkey3,String where3,IDictionary
{
Boolean ret = false;
Axapta ax = new Axapta();
NetworkCredential nc = new NetworkCredential( "kurodo ", " abc@123 ", "kurodo .cn ");
try
{
ax.LogonAs( "kurodo ", "kurodo ", nc, null, null, null, null);
IEnumerator
using (AxaptaRecord axRecord = ax.CreateAxaptaRecord(tableName))
{
String sql = String.Empty;
ax.TTSBegin();
if (pkey3 != null)
{
sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' && %1.{2} == {3} && %1.{4} == {5} ", pkey1, where1,pkey2,where2,pkey3,where3);
}
else if (pkey2 != null)
{
sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' && %1.{2} == {3} ", pkey1, where1, pkey2, where2);
}
else
{
sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' ", pkey1, where1);
}
axRecord.ExecuteStmt(sql);
if (axRecord.Found)
{
while (dem.MoveNext())
{
string key = dem.Current.Key;
Object value = dem.Current.Value;
axRecord.set_Field(key, value);
}
// if (axRecord.ValidateWrite())
// {
axRecord.Update();
ax.TTSCommit();
ret = true;
// }
}
else
ax.TTSAbort();
}
ax.Logoff();
}
catch (Exception ex)
{
ax.Logoff();
throw new Exception(ex.Message);
}
return ret;
}
///
/// Select Method
///
/// 表名称
/// 键名称
/// 条件值
/// 字段列表
///
public static IList