[Serializable]
public class CSqlParameters : ISerializable
{
private string m_strSqlText;
private ArrayList m_arrayParams;
public CSqlParameters()
{
m_strSqlText = "";
m_arrayParams = new ArrayList();
}
public void Add(SqlParameter newParam)
{
m_arrayParams.Add(newParam);
}
public string SqlText
{
set { m_strSqlText = value; }
get { return m_strSqlText; }
}
public ArrayList Parameters
{
get { return m_arrayParams; }
}
#region "For Serialize Support"
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("SqlText", m_strSqlText);
info.AddValue("ParamCount", m_arrayParams.Count);
for (int i = 0; i < m_arrayParams.Count; ++i)
{
info.AddValue("ParamName" + i.ToString(), ((SqlParameter)m_arrayParams[i]).ParameterName);
info.AddValue("ParamSize" + i.ToString(), ((SqlParameter)m_arrayParams[i]).Size);
info.AddValue("ParamValue" + i.ToString(), ((SqlParameter)m_arrayParams[i]).Value.ToString());
}
}
protected CSqlParameters(SerializationInfo info, StreamingContext context)
{
m_arrayParams = new ArrayList();
m_strSqlText = info.GetString("SqlText");
int count = info.GetInt32("ParamCount");
for (int i = 0; i < count; ++i)
{
string paramName = info.GetString("ParamName" + i.ToString());
int paramSize = info.GetInt32("ParamSize" + i.ToString());
object paramValue = info.GetString("ParamValue" + i.ToString());
SqlParameter sqlParam = new SqlParameter(paramName, paramValue);
sqlParam.Size = paramSize;
m_arrayParams.Add(sqlParam);
}
}
#endregion
}
看代码是处理SQL参数的
谢谢
应该是 一个 对 SqlParameter 进行序列化反序列化.
赋值:
CSqlParameters p = new CSqlParameters();
p.SqlText = "select * from aspnet_Memberships where UserName=@UserName";
p.Add(new SqlParameter("@UsrName", "admin");
序列化调用的是:GetObjectData(SerializationInfo info, StreamingContext context)
反序列化会调用:protected CSqlParameters(SerializationInfo info, StreamingContext context)
怎么序列化和反序列,可以看下ISerializable用法
谢谢