相关文章推荐

How to serialize SqlParameter ?

hi, I would like to ask how to serialize class with members like
SqlParameter. I try to use BinaryFormatter.

[Serializable]
public class Example_Class
public SqlParameter sqlParam;

public Example_Class()
sqlParam = new SqlParameter("@Data", SqlDbType.VarChar);
class App
[STAThread]
static void Main()
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

// Construct a BinaryFormatter and use it
// to serialize the data to the stream.
BinaryFormatter formatter = new

Example_Class dod = new Example_Class();
// Serialize the array elements.
formatter.Serialize(fs, dod);

// Deserialize the array elements.
fs.Position = 0;
Example_Class dod2 = (Example_Class) formatter.Deserialize(fs);
Console.WriteLine("Do both class refer to the same object? "
+ (dod.sqlParam == dod2.sqlParam));
Console.ReadLine();

catch (SerializationException e)
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
Console.ReadLine();
throw;
finally
fs.Close();
and error is :

"The type System.Data.SqlClient.SqlParameter in Assembly System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable."

but I know that's possible. What I do wrong ?
How do I have to implement this?

Thanks,
Marcin

Nov 16 '05 # 1
1 7445
Marcin,

You would have to mark the field with the SqlParameter as
NonSerializable (with the attribute). Then, you can implement the
IDerializationCallback interface to be notified when deserialization is
complete, so that you can re-initialize it.

Hope this helps.
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marcin" <ma*********@poczta.onet.pl> wrote in message
news:cf**********@atlantis.news.tpi.pl...
hi, I would like to ask how to serialize class with members like
SqlParameter. I try to use BinaryFormatter.

[Serializable]
public class Example_Class
{

public SqlParameter sqlParam;

public Example_Class()
{
sqlParam = new SqlParameter("@Data", SqlDbType.VarChar);
}
}

class App
{
[STAThread]
static void Main()
{
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

try
{
// Construct a BinaryFormatter and use it
// to serialize the data to the stream.
BinaryFormatter formatter = new

Example_Class dod = new Example_Class();
// Serialize the array elements.
formatter.Serialize(fs, dod);

// Deserialize the array elements.
fs.Position = 0;
Example_Class dod2 = (Example_Class) formatter.Deserialize(fs);

Console.WriteLine("Do both class refer to the same object? "
+ (dod.sqlParam == dod2.sqlParam));
Console.ReadLine();

}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message); Console.ReadLine();
throw;
}
finally
{
fs.Close();
}
}

and error is :

"The type System.Data.SqlClient.SqlParameter in Assembly System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable."

but I know that's possible. What I do wrong ?
How do I have to implement this?

Thanks,
Marcin

Nov 16 '05 # 2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Kenneth | last post by: Can anyone explain me why it is neccesary to include SqlDbType to the SqlParameter. In every example I see, it is done, but no one explaines I have for example a date I want to save into my...
by: confused | last post by: I want to assign a default value of DBNull.Value to my stored procedure parameter, but also have the ability to overwrite it, so: SqlParameter param = new SqlParameter...
by: Daniel Groh | last post by: What is the best way to access a procedure ? MyCommand.Parameters.Add("@param1",typeof(string)); SqlParameter myParam = new SqlParameter(); myParam.Add("@param1",typeof(string)); What...
by: Jason Huang | last post by: I want to use the Sqlparameter and SqlDataAdapter to update my data, and the data will be updated based on two TextBoxes txtCustName and txtCustAddress. Thanks for help. Jason
by: Jason Huang | last post by: The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is...
by: Elliot M. Rodriguez | last post by: I implemented a very small, basic data access layer for my web application. It works just fine, except for this one bug. One of my methods returns an abstracted dataset. To accomodate X number of...
by: Stacey Levine | last post by: I have a webservice that has the below procedure. Basically a procedure to called a stored procedure and return the results. When I try to call the webservice from my program I get the error. Both...
by: Mirek Endys | last post by: What is the best way to serialize object, that contains data in interfaces. Simple. I have instance of my object, that contains data in interfaces. What is the best way to XMLSerialize and...
by: Tim Zych | last post by: ' Declare a new parameter object Dim param() As SqlParameter = New SqlParameter(0) {} ' Set this to null and make it an InputOutput parameter param(0) = New SqlParameter("@Something, DBNull.Value)...
by: davi5007 | last post by: Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by: I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: Aftab Ahmad | last post by: Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
by: Aftab Ahmad | last post by: So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
by: ryjfgjl | last post by: ExcelToDatabase: batch import excel into database automatically...
by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
by: Vimpel783 | last post by: Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
by: jfyes | last post by: As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
by: ArrayDB | last post by: The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use .

 
推荐文章