469,636 Members | 1,970 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

serialize SqlCommand

job
how is it possible to serialize/de-serialize a SqlCommand?
Sep 20 '06 #1
1 6625
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Hervé RESCOURIO | last post: by
reply views Thread by Hervé RESCOURIO | last post: by
5 posts views Thread by David Sworder | last post: by
10 posts views Thread by Dan | last post: by
1 post views Thread by js | last post: by
reply views Thread by Redowl | last post: by
4 posts views Thread by =?Utf-8?B?Qnlyb24=?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.