INSERT INTO fails for SqlDbType.Text

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to inser a Text string as a BLOB
When I try to insert a series of records I get the following problem
-- System.Data.SqlClient.SqlException: String or binary data would be truncated

My table has three nvarchar 50 columns, (MessageId, PutDate, MsgType
and a Text column (RawMessage), which I want to use as large storage container for XML messages

So I read the XML Messages and want to insert each into the Database

The code of the INSERT Part looks like this

private static void InsertIntoDB(byte[] msgId, DateTime putDate, string message

tr

string commandText = "INSERT INTO message (MessageId, PutDate, MsgType, RawMessage) "
"VALUES ('"+BitConverter.ToString( msgId )+"','"+putDate.ToString("u")+"','Type',@RawMessage)"
command.Connection = con
command.CommandText = commandText
command.Parameters.Clear()
SqlParameter msg = new SqlParameter("@RawMessage",SqlDbType.Text,message.Length)
msg.Value = message
command.Parameters.Add( msg )

command.ExecuteNonQuery()

catch(Exception e

Console.WriteLine(e.ToString())
Console.WriteLine("Neither record is written to the database!")




Any Help?
 
Hi Pete,

Yes, you will have to use parametrized insert.
See SqlParameter .net help or check articles at Ryan's www.knowdotnet.com...

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Pete said:
I want to inser a Text string as a BLOB.
When I try to insert a series of records I get the following problem
-- System.Data.SqlClient.SqlException: String or binary data would be truncated.

My table has three nvarchar 50 columns, (MessageId, PutDate, MsgType )
and a Text column (RawMessage), which I want to use as large storage container for XML messages.

So I read the XML Messages and want to insert each into the Database.

The code of the INSERT Part looks like this:

private static void InsertIntoDB(byte[] msgId, DateTime putDate, string message)
{
try
{
string commandText = "INSERT INTO message (MessageId, PutDate, MsgType, RawMessage) "+
"VALUES ('"+BitConverter.ToString(
msgId )+"','"+putDate.ToString("u")+"','Type',@RawMessage)";
 
Back
Top