在SPS的对象模型中提供了方法修改扩展字段内容的方法,可是用这种方法修改
扩展字段会产生文档版本。可以用以下的方法直接去修改SPS数据库字段。
代码如下:
1、SpsExpendFieldData 类
namespace UltraPower.SPS.DB
{
/// <summary>
/// 每个扩展字段的属性信息,此类属于纯数据类不存在任何操作
/// </summary>
public class SpsExpendFieldData
{
#region 域
private string m_ColsName;
private string m_DataType;
private string m_Name;
private int m_Size;
#endregion
#region 属性
/// <summary>
/// 扩展字段在SPS数据库中对应的名称
/// </summary>
public string ColsName
{
get
{
/*Begin Template Expansion{63A03D96-E292-4D92-805C-0C2D134BC8F9}*/
return m_ColsName;
/*End Template Expansion{63A03D96-E292-4D92-805C-0C2D134BC8F9}*/
}
set
{
/*Begin Template Expansion{EBA66CF2-6644-41AF-B8C7-F85E292F4041}*/
m_ColsName = value;
/*End Template Expansion{EBA66CF2-6644-41AF-B8C7-F85E292F4041}*/
}
}
/// <summary>
/// 数据类型
/// </summary>
public string DataType
{
get
{
/*Begin Template Expansion{88374051-9EC0-4D85-BABC-630A275F2F93}*/
return m_DataType;
/*End Template Expansion{88374051-9EC0-4D85-BABC-630A275F2F93}*/
}
set
{
/*Begin Template Expansion{5FCBBB45-9E62-495D-AE1E-A485DE07B436}*/
m_DataType = value;
/*End Template Expansion{5FCBBB45-9E62-495D-AE1E-A485DE07B436}*/
}
}
/// <summary>
/// 扩展字段的名称
/// </summary>
public string Name
{
get
{
/*Begin Template Expansion{CFEAAA3E-1597-4048-BF7B-14BE1B9BD033}*/
return m_Name;
/*End Template Expansion{CFEAAA3E-1597-4048-BF7B-14BE1B9BD033}*/
}
set
{
/*Begin Template Expansion{F9F30932-0F45-4D80-AD1D-1F280051431D}*/
m_Name = value;
/*End Template Expansion{F9F30932-0F45-4D80-AD1D-1F280051431D}*/
}
}
/// <summary>
/// 数据长度
/// </summary>
public int Size
{
get
{
/*Begin Template Expansion{F4243132-8D17-40B0-ABD0-6DC416AF20D0}*/
return m_Size;
/*End Template Expansion{F4243132-8D17-40B0-ABD0-6DC416AF20D0}*/
}
set
{
/*Begin Template Expansion{8C930AE2-2151-4CE6-8397-9B157CD0B0EB}*/
m_Size = value;
/*End Template Expansion{8C930AE2-2151-4CE6-8397-9B157CD0B0EB}*/
}
}
#endregion
#region 构造器
private SpsExpendFieldData()
{
}
#endregion
#region 方法
/// <summary>
/// 静态方法返回一个当前类的实例
/// </summary>
public static SpsExpendFieldData Create(string name, string colsName, int size, string dataType)
{
SpsExpendFieldData obj = new SpsExpendFieldData();
obj.Name = name;
obj.ColsName = colsName;
obj.Size = size;
obj.DataType = dataType;
return obj;
}
public static SpsExpendFieldData Create(string name, string colsName, int size)
{
return Create(name,colsName,size,null);
}
public static SpsExpendFieldData Create(string name, string colsName)
{
return Create(name,colsName,0,null);
}
#endregion
}
}
2、SpsExpendFields类
using System;
using System.Data;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
using UltraPower.DB.SqlClient.DataAccess;
namespace UltraPower.SPS.DB
{
/// <summary>
/// 直接在SPS文档库中修改扩展字段值
/// </summary>
public class SpsExpendFields
{
#region 域
private string m_DBConnStr;
private string m_DocLibGuid;
private string m_FieldStr;
private DataBase m_DB;
private ArrayList m_ExpendFieldsNameValueList;
#endregion
#region 属性
/// <summary>
/// SPS文挡库数据库连接字符串
/// </summary>
public string DBConnStr
{
get
{
/*Begin Template Expansion{8BD87766-C6FF-4504-B59F-2B41DFC62C61}*/
return m_DBConnStr;
/*End Template Expansion{8BD87766-C6FF-4504-B59F-2B41DFC62C61}*/
}
set
{
/*Begin Template Expansion{E4B4BE05-2005-4089-96AB-32A3F08A7D78}*/
m_DBConnStr = value;
/*End Template Expansion{E4B4BE05-2005-4089-96AB-32A3F08A7D78}*/
}
}
public string DocLibGuid
{
get
{
/*Begin Template Expansion{7797A678-53AC-4260-A590-9ACB53328CB4}*/
return m_DocLibGuid;
/*End Template Expansion{7797A678-53AC-4260-A590-9ACB53328CB4}*/
}
set
{
/*Begin Template Expansion{F9649A26-B95E-476E-82D0-736EE7F19EA4}*/
m_DocLibGuid = value;
/*End Template Expansion{F9649A26-B95E-476E-82D0-736EE7F19EA4}*/
}
}
public string FieldStr
{
get
{
if (m_FieldStr != null)
return m_FieldStr;
else
return "";
}
}
public DataBase DB
{
get
{
if (m_DB == null)
{
m_DB = new DataBase(DBConnStr);
}
return m_DB;
}
}
public ArrayList ExpendFieldsNameValueList
{
get
{
if (m_ExpendFieldsNameValueList == null)
{
GetFieldStr();
m_ExpendFieldsNameValueList = GetFieldInfo();
}
return m_ExpendFieldsNameValueList;
}
}
#endregion
#region 构造函数
public SpsExpendFields()
{
}
public SpsExpendFields(string dbConnStr)
{
DBConnStr = dbConnStr;
}
public SpsExpendFields(string dbConnStr,string docLibGuid)
{
DBConnStr = dbConnStr;
DocLibGuid = docLibGuid;
}
#endregion
#region 私有方法
private string GetFieldStr()
{
string strSQL = "SELECT Tp_Fields FROM Lists WHERE Tp_ID = '" + DocLibGuid + "'";
m_FieldStr = DB.ReturnValue(strSQL);
return FieldStr;
}
/// <summary>
/// 得到扩展字段的属性信息
/// </summary>
private ArrayList GetFieldInfo()
{
try
{
ArrayList result = new ArrayList();
GetFieldStr();
XmlDocument xd = new XmlDocument();
xd.LoadXml("<myroot>" + FieldStr + "</myroot>");
XPathNavigator xpn = xd.CreateNavigator();
XPathNodeIterator ni = xpn.Select("/myroot/Field");
while (ni.MoveNext())
{
SpsExpendFieldData objFieldData = SpsExpendFieldData.Create(ni.Current.GetAttribute("Name",""),
ni.Current.GetAttribute("ColName",""),
0,
ni.Current.GetAttribute("Type",""));
result.Add(objFieldData);
}
return result;
}
catch(Exception e)
{
throw e;
}
}
#endregion
#region 公共方法
/// <summary>
/// 根据扩展字段的名称得到Sps数据库中实际的字段名
/// </summary>
/// <param name="fieldName"></param>
/// <returns></returns>
public string GetDBFieldName(string fieldName)
{
string result = "";
IEnumerator obj = ExpendFieldsNameValueList.GetEnumerator();
while(obj.MoveNext())
{
if (((SpsExpendFieldData)obj.Current).Name.Equals(fieldName))
{
result = ((SpsExpendFieldData)obj.Current).ColsName;
}
}
return result;
}
/// <summary>
/// 根据扩展字段的名称得到SpsExpendFieldData对象(包含扩展字段的信息)
/// </summary>
/// <param name="fieldName"></param>
/// <returns></returns>
public SpsExpendFieldData GetExpendFieldData(string fieldName)
{
SpsExpendFieldData result = null;
IEnumerator obj = ExpendFieldsNameValueList.GetEnumerator();
while(obj.MoveNext())
{
if (((SpsExpendFieldData)obj.Current).Name.Equals(fieldName))
{
result = (SpsExpendFieldData)obj.Current;
}
}
return result;
}
#endregion
}
}
3、SpsExpendFieldsModify 类
using System.Data;
using System.Collections;
using System.Text;
using UltraPower.DB.SqlClient.DataAccess;
namespace UltraPower.SPS.DB
{
/// <summary>
/// 修改扩展字段内容
/// </summary>
public class SpsExpendFieldsModify
{
#region 域
private string m_DBConnStr;
private string m_DocLibGuid;
private DataBase m_DB;
#endregion
#region 属性
/// <summary>
/// SPS数据库连接字符串
/// </summary>
public string DBConnStr
{
get
{
return m_DBConnStr;
}
set
{
m_DBConnStr = value;
}
}
public string DocLibGuid
{
get
{
return m_DocLibGuid;
}
set
{
m_DocLibGuid = value;
}
}
public DataBase DB
{
get
{
if (m_DB == null)
{
m_DB = new DataBase(DBConnStr);
}
return m_DB;
}
}
#endregion
#region 构造器
public SpsExpendFieldsModify(string dbConnStr,string docLibGuid)
{
DBConnStr = dbConnStr;
DocLibGuid = docLibGuid;
}
public SpsExpendFieldsModify()
{
}
#endregion
#region 私有方法
/// <summary>
/// 生成更新扩展字段的Sql语句
/// </summary>
/// <param name="docID"></param>
/// <param name="updateData"></param>
/// <returns></returns>
private string CreateUpdateSql(int docID, Hashtable updateData)
{
SpsExpendFields objExpendFields = new SpsExpendFields(DBConnStr,DocLibGuid);
StringBuilder result = new StringBuilder();
result.Append("Update UserData Set Tp_ID = Tp_ID");
if (updateData.Count > 0)
{
IDictionaryEnumerator obj = updateData.GetEnumerator();
while(obj.MoveNext())
{
if (objExpendFields.GetDBFieldName(obj.Key.ToString()).IndexOf("nvarchar")==-1)
{
result.Append( "," + objExpendFields.GetDBFieldName(obj.Key.ToString()) + " = '" + obj.Value.ToString()+"'");
}
else
{
result.Append( "," + objExpendFields.GetDBFieldName(obj.Key.ToString()) + " = N'" + obj.Value.ToString()+"'");
}
}
result.Append(" Where Tp_ID = " + docID.ToString() + " And Tp_ListID = '" + DocLibGuid + "'");
}
return result.ToString();
}
#endregion
#region 公共方法
/// <summary>
/// 执行更新扩展字段的操作
/// </summary>
/// <param name="docID"></param>
/// <param name="updateData"></param>
/// <returns></returns>
public bool Update(int docID,Hashtable updateData)
{
try
{
DB.RunProc(CreateUpdateSql(docID,updateData));
return true;
}
catch
{
return false;
}
}
#endregion
}
}
使用示例:
SpsExpendFieldsModify obj = new SpsExpendFieldsModify();
//要操作文档库的GUID
obj.DocLibGuid = "b1dd10ae-f9c0-494d-8296-182c82762b85";
//要操作文档库的数据库联接字符串
obj.DBConnStr = "Data Source=10.1.30.44;User Id=sa;Password=**;Initial Catalog=DEVSps1_Site;";
System.Collections.Hashtable expendFields = new Hashtable();
expendFields .Add("expendfield1","11");
expendFields .Add("expendfield2","dafsdfasdf1");
expendFields .Add("expendfield3","4");
obj.Update(3,expendFields );