现在想问有没有更直接的方法,直接将信息保存到数据库(要用什么Stream类,怎样用?),另外还要问保存给序列化信息需要什么数据类型才行:备注类型还是OLE对象。贴出xml序列化的实现方法也行!
7 个解决方案
#1
与数据库不好交流的哦,
#2
序列化
//序列化
public byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
//序列化
public byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
#3
如果你只是直接将信息保存到数据库,沒有必要序列化
string conn_str = System.Configuration.ConfigurationSettings.AppSettings["conn_str"].ToString();
string sREQUEST_ID = Session["REQUEST_ID"].ToString();
OleDbConnection cn = new OleDbConnection(conn_str);
cn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE REQUEST_MASTER SET REQUEST_FILE=? WHERE REQUEST_ID=" +int.Parse(sREQUEST_ID),cn);
cmd.Parameters.Add("REQUEST_FILE",OleDbType.Binary);
if(!sFileName1.Equals(""))
{
try
{
FileStream fs = new FileStream(Path +sFileName1, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData1= new byte[fs.Length];
fs.Read(MyData1, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
cmd.Parameters["REQUEST_FILE"].Value = MyData1;
}
catch(Exception err)
{
string s = err.Message;
}
}
else
{
cmd.Parameters["REQUEST_FILE"].Value = MyData;
}
try
{
cmd.ExecuteNonQuery();
}
catch(Exception e1)
{
}
finally
{
cn.Close();
}
string conn_str = System.Configuration.ConfigurationSettings.AppSettings["conn_str"].ToString();
string sREQUEST_ID = Session["REQUEST_ID"].ToString();
OleDbConnection cn = new OleDbConnection(conn_str);
cn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE REQUEST_MASTER SET REQUEST_FILE=? WHERE REQUEST_ID=" +int.Parse(sREQUEST_ID),cn);
cmd.Parameters.Add("REQUEST_FILE",OleDbType.Binary);
if(!sFileName1.Equals(""))
{
try
{
FileStream fs = new FileStream(Path +sFileName1, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData1= new byte[fs.Length];
fs.Read(MyData1, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
cmd.Parameters["REQUEST_FILE"].Value = MyData1;
}
catch(Exception err)
{
string s = err.Message;
}
}
else
{
cmd.Parameters["REQUEST_FILE"].Value = MyData;
}
try
{
cmd.ExecuteNonQuery();
}
catch(Exception e1)
{
}
finally
{
cn.Close();
}
#4
ACCESS用OLE,至于写入和读取和普通的二进制操作一样,序列化和反序列化的方法楼上的几们都说了,不会可以看看SDK.
#5
以下的类是string和object之间的转换
对于如果string保存到数据中,楼主应该明白
/// <summary>
/// 对象序列化对象类
/// </summary>
public class Serializable
{
private Serializable()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 把对象序列化并返回相应的字节
/// </summary>
/// <param name="pObj">需要序列化的对象</param>
/// <returns>byte[]</returns>
public static byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
public static string SerializeObjectToString(object pOjb)
{
Byte[] bytes = SerializeObject(pOjb);
return System.Convert.ToBase64String(bytes);
}
/// <summary>
/// 把字节反序列化成相应的对象
/// </summary>
/// <param name="pBytes">字节流</param>
/// <returns>object</returns>
public static object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if(pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
public static object DeserializeObjectByString(string text)
{
Byte[] bytes = System.Convert.FromBase64String(text);
return DeserializeObject(bytes);
}
}
对于如果string保存到数据中,楼主应该明白
/// <summary>
/// 对象序列化对象类
/// </summary>
public class Serializable
{
private Serializable()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 把对象序列化并返回相应的字节
/// </summary>
/// <param name="pObj">需要序列化的对象</param>
/// <returns>byte[]</returns>
public static byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
public static string SerializeObjectToString(object pOjb)
{
Byte[] bytes = SerializeObject(pOjb);
return System.Convert.ToBase64String(bytes);
}
/// <summary>
/// 把字节反序列化成相应的对象
/// </summary>
/// <param name="pBytes">字节流</param>
/// <returns>object</returns>
public static object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if(pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
public static object DeserializeObjectByString(string text)
{
Byte[] bytes = System.Convert.FromBase64String(text);
return DeserializeObject(bytes);
}
}
#6
有那么复杂吗?数据库定义一个超长的字符串
System.Convert.ToBase64String(byte[]) ----string
System.Convert.FromBase64String(string s) ---byte[]
System.Convert.ToBase64String(byte[]) ----string
System.Convert.FromBase64String(string s) ---byte[]
#7
正是我想要的,谢谢!
#1
与数据库不好交流的哦,
#2
序列化
//序列化
public byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
//序列化
public byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
#3
如果你只是直接将信息保存到数据库,沒有必要序列化
string conn_str = System.Configuration.ConfigurationSettings.AppSettings["conn_str"].ToString();
string sREQUEST_ID = Session["REQUEST_ID"].ToString();
OleDbConnection cn = new OleDbConnection(conn_str);
cn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE REQUEST_MASTER SET REQUEST_FILE=? WHERE REQUEST_ID=" +int.Parse(sREQUEST_ID),cn);
cmd.Parameters.Add("REQUEST_FILE",OleDbType.Binary);
if(!sFileName1.Equals(""))
{
try
{
FileStream fs = new FileStream(Path +sFileName1, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData1= new byte[fs.Length];
fs.Read(MyData1, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
cmd.Parameters["REQUEST_FILE"].Value = MyData1;
}
catch(Exception err)
{
string s = err.Message;
}
}
else
{
cmd.Parameters["REQUEST_FILE"].Value = MyData;
}
try
{
cmd.ExecuteNonQuery();
}
catch(Exception e1)
{
}
finally
{
cn.Close();
}
string conn_str = System.Configuration.ConfigurationSettings.AppSettings["conn_str"].ToString();
string sREQUEST_ID = Session["REQUEST_ID"].ToString();
OleDbConnection cn = new OleDbConnection(conn_str);
cn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE REQUEST_MASTER SET REQUEST_FILE=? WHERE REQUEST_ID=" +int.Parse(sREQUEST_ID),cn);
cmd.Parameters.Add("REQUEST_FILE",OleDbType.Binary);
if(!sFileName1.Equals(""))
{
try
{
FileStream fs = new FileStream(Path +sFileName1, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData1= new byte[fs.Length];
fs.Read(MyData1, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
cmd.Parameters["REQUEST_FILE"].Value = MyData1;
}
catch(Exception err)
{
string s = err.Message;
}
}
else
{
cmd.Parameters["REQUEST_FILE"].Value = MyData;
}
try
{
cmd.ExecuteNonQuery();
}
catch(Exception e1)
{
}
finally
{
cn.Close();
}
#4
ACCESS用OLE,至于写入和读取和普通的二进制操作一样,序列化和反序列化的方法楼上的几们都说了,不会可以看看SDK.
#5
以下的类是string和object之间的转换
对于如果string保存到数据中,楼主应该明白
/// <summary>
/// 对象序列化对象类
/// </summary>
public class Serializable
{
private Serializable()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 把对象序列化并返回相应的字节
/// </summary>
/// <param name="pObj">需要序列化的对象</param>
/// <returns>byte[]</returns>
public static byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
public static string SerializeObjectToString(object pOjb)
{
Byte[] bytes = SerializeObject(pOjb);
return System.Convert.ToBase64String(bytes);
}
/// <summary>
/// 把字节反序列化成相应的对象
/// </summary>
/// <param name="pBytes">字节流</param>
/// <returns>object</returns>
public static object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if(pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
public static object DeserializeObjectByString(string text)
{
Byte[] bytes = System.Convert.FromBase64String(text);
return DeserializeObject(bytes);
}
}
对于如果string保存到数据中,楼主应该明白
/// <summary>
/// 对象序列化对象类
/// </summary>
public class Serializable
{
private Serializable()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 把对象序列化并返回相应的字节
/// </summary>
/// <param name="pObj">需要序列化的对象</param>
/// <returns>byte[]</returns>
public static byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
public static string SerializeObjectToString(object pOjb)
{
Byte[] bytes = SerializeObject(pOjb);
return System.Convert.ToBase64String(bytes);
}
/// <summary>
/// 把字节反序列化成相应的对象
/// </summary>
/// <param name="pBytes">字节流</param>
/// <returns>object</returns>
public static object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if(pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
public static object DeserializeObjectByString(string text)
{
Byte[] bytes = System.Convert.FromBase64String(text);
return DeserializeObject(bytes);
}
}
#6
有那么复杂吗?数据库定义一个超长的字符串
System.Convert.ToBase64String(byte[]) ----string
System.Convert.FromBase64String(string s) ---byte[]
System.Convert.ToBase64String(byte[]) ----string
System.Convert.FromBase64String(string s) ---byte[]
#7
正是我想要的,谢谢!