473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

serialize SqlCommand

job
how is it possible to serialize/de-serialize a SqlCommand?
Sep 20 '06 #1
1 7192
Hi,

It's not possible to serialize an SqlCommand. However, you can serialize an instance of a custom class instead that contains all of
the relevant information for recreating the SqlCommand.

Here's an example that partially serializes an SqlCommand by storing the command Text, Type and Timeout properties and recreating
the SqlCommand after deserialization:

[Serializable]
public sealed class SqlCommandMessage
{
[XmlIgnore]
public SqlCommand Command
{
get
{
if (command == null)
CreateCommand();

return command;
}
}

public string CommandText
{
get
{
return commandText;
}
set
{
commandText = value;
}
}

public CommandType CommandType
{
get
{
return commandType;
}
set
{
commandType = value;
}
}

public int CommandTimeout
{
get
{
return commandTimeout;
}
set
{
commandTimeout = value;
}
}

[NonSerialized]
private SqlCommand command;
private string commandText;
private CommandType commandType;
private int commandTimeout;

/// <summary>
/// Parameterless constructor is required for xml serialization
/// </summary>
private SqlCommandMessage()
{
}

public SqlCommandMessage(SqlCommand command)
{
if (command == null)
throw new ArgumentNullException("command");

this.command = command;

commandText = command.CommandText;
commandTimeout = command.CommandTimeout;
commandType = command.CommandType;
}

private void CreateCommand()
{
command = new SqlCommand(commandText);

command.CommandTimeout = commandTimeout;
command.CommandType = commandType;
}
}
-- console program unit test --

class Program
{
static void Main(string[] args)
{
using (SqlCommand command = new SqlCommand())
{
// initialize the command
command.CommandText = "SomeStoredProc";
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 15;

// create stream to hold the serialized SqlCommandMessage
using (MemoryStream stream = new MemoryStream())
{
// create the message
SqlCommandMessage message = new SqlCommandMessage(command);

// binary serialization
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, message);

// xml serialization
XmlSerializer serializer = new XmlSerializer(typeof(SqlCommandMessage));
serializer.Serialize(stream, message);

stream.Position = 0;

// read binary
message = (SqlCommandMessage) formatter.Deserialize(stream);

Console.WriteLine("Deserialized binary command: {0}", message.Command != command);
Console.WriteLine("Text: " + message.Command.CommandText);
Console.WriteLine("Type: " + message.Command.CommandType);
Console.WriteLine("Timeout: " + message.Command.CommandTimeout);

Console.WriteLine();

// read xml
message = (SqlCommandMessage) serializer.Deserialize(stream);

Console.WriteLine("Deserialized xml command: {0}", message.Command != command);
Console.WriteLine("Text: " + message.Command.CommandText);
Console.WriteLine("Type: " + message.Command.CommandType);
Console.WriteLine("Timeout: " + message.Command.CommandTimeout);

Console.ReadLine();
}
}

//RegexTest();
}
}

--
Dave Sexton

<jo*@bla.comwrote in message news:pi********************************@4ax.com...
how is it possible to serialize/de-serialize a SqlCommand?

Sep 20 '06 #2

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

Similar topics

0
by: Hervé RESCOURIO | last post by:
Hi, I want to serialize SqlCommand in a component, but my sample doesn't work. Any idea? public partial class SglQuery : Component { public SglQuery() { InitializeComponent();
0
by: Hervé RESCOURIO | last post by:
Hi, I want to serialize a SqlCommand object in a component, but my sample doesn't work. Have you any idea? public partial class SglQuery : Component { public SglQuery() {
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
1
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. ...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
0
by: Redowl | last post by:
Hi, I am trying to serialize a business object using xml generated by a FOR XML query and the resulting xml has no root element. I have created my business object using the schema returned from...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.