TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

Notices

Question on inserting varbinary to SQL table via Powershell

Microsoft Windows Powershell


Reply
 
Thread Tools Display Modes
Old 26-06-2008, 04:31 PM   #1
Frank
Guest
 
Posts: n/a
Question on inserting varbinary to SQL table via Powershell

Hi,

I apologize if this is the wrong discussion group but its a question of how
to insert a varbinary (word doc) into SQL. I actually got this to work but
need to understand why it works one way and not the other. Here is the
procedure which works:
==============================================
[byte[]]$file = Get-Content d:\test.docx -Encoding byte
$DB = "server=sqlserver01;integrated security=sspi;database=test"

$conn = new-object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = $DB
$conn.Open()
$cmd = new-object System.Data.SqlClient.SqlCommand
$cmd.Connection=$conn
$cmd.CommandText="insert into binarytable (binary,datetime,type,service)
values(@FileData,'$datetime','$type','$service');"
$cmd.Parameters.Add("@FileData", $file)
$rdr = $cmd.ExecuteReader() | out-null 2>&1
====================================

What doesn't work is using an insert statement and use the $file variable I
set earlier, ie:

$cmd.CommandText="insert into binarytable (binary,datetime,type,service)
values('$file','$datetime','$type','$service');"

The "$cmd.Parameters.Add("@FileData", $file)" line must do some sort of
conversion which enables this but I would like to understand this a little
better.

Can anyone help?

Thanks in advance,





  Reply With Quote
Old 26-06-2008, 04:32 PM   #2
Leo Tohill
Guest
 
Posts: n/a
RE: Question on inserting varbinary to SQL table via Powershell

Hello Frank,

$cmd.CommandText="insert into binarytable (binary,datetime,type,service)
values(@FileData,'$datetime','$type','$service');"

builds a string literal as the insert statement. Binary values simply
cannot be represented in a string literal. Using a parameter, as you have
done, is the most common approach to inserting binary data. Another
approach is hex-encoding the data. If you hex-encoded the data with the
right escape characters for sql server, then you could place it as a literal
into the insert, but
why bother?

I'm puzzled why you use ExecuteReader() instead of ExecuteNonQuery().

"Frank" wrote:

> Hi,
>
> I apologize if this is the wrong discussion group but its a question of how
> to insert a varbinary (word doc) into SQL. I actually got this to work but
> need to understand why it works one way and not the other. Here is the
> procedure which works:
> ==============================================
> [byte[]]$file = Get-Content d:\test.docx -Encoding byte
> $DB = "server=sqlserver01;integrated security=sspi;database=test"
>
> $conn = new-object System.Data.SqlClient.SqlConnection
> $conn.ConnectionString = $DB
> $conn.Open()
> $cmd = new-object System.Data.SqlClient.SqlCommand
> $cmd.Connection=$conn
> $cmd.CommandText="insert into binarytable (binary,datetime,type,service)
> values(@FileData,'$datetime','$type','$service');"
> $cmd.Parameters.Add("@FileData", $file)
> $rdr = $cmd.ExecuteReader() | out-null 2>&1
> ====================================
>
> What doesn't work is using an insert statement and use the $file variable I
> set earlier, ie:
>
> $cmd.CommandText="insert into binarytable (binary,datetime,type,service)
> values('$file','$datetime','$type','$service');"
>
> The "$cmd.Parameters.Add("@FileData", $file)" line must do some sort of
> conversion which enables this but I would like to understand this a little
> better.
>
> Can anyone help?
>
> Thanks in advance,
>
>
>
>
>

  Reply With Quote
Old 26-06-2008, 04:32 PM   #3
Frank
Guest
 
Posts: n/a
RE: Question on inserting varbinary to SQL table via Powershell

I didn't know there was ExecuteNonQuery(). What is the difference?

Thanks!



"Leo Tohill" wrote:

> Hello Frank,
>
> $cmd.CommandText="insert into binarytable (binary,datetime,type,service)
> values(@FileData,'$datetime','$type','$service');"
>
> builds a string literal as the insert statement. Binary values simply
> cannot be represented in a string literal. Using a parameter, as you have
> done, is the most common approach to inserting binary data. Another
> approach is hex-encoding the data. If you hex-encoded the data with the
> right escape characters for sql server, then you could place it as a literal
> into the insert, but
> why bother?
>
> I'm puzzled why you use ExecuteReader() instead of ExecuteNonQuery().
>
> "Frank" wrote:
>
> > Hi,
> >
> > I apologize if this is the wrong discussion group but its a question of how
> > to insert a varbinary (word doc) into SQL. I actually got this to work but
> > need to understand why it works one way and not the other. Here is the
> > procedure which works:
> > ==============================================
> > [byte[]]$file = Get-Content d:\test.docx -Encoding byte
> > $DB = "server=sqlserver01;integrated security=sspi;database=test"
> >
> > $conn = new-object System.Data.SqlClient.SqlConnection
> > $conn.ConnectionString = $DB
> > $conn.Open()
> > $cmd = new-object System.Data.SqlClient.SqlCommand
> > $cmd.Connection=$conn
> > $cmd.CommandText="insert into binarytable (binary,datetime,type,service)
> > values(@FileData,'$datetime','$type','$service');"
> > $cmd.Parameters.Add("@FileData", $file)
> > $rdr = $cmd.ExecuteReader() | out-null 2>&1
> > ====================================
> >
> > What doesn't work is using an insert statement and use the $file variable I
> > set earlier, ie:
> >
> > $cmd.CommandText="insert into binarytable (binary,datetime,type,service)
> > values('$file','$datetime','$type','$service');"
> >
> > The "$cmd.Parameters.Add("@FileData", $file)" line must do some sort of
> > conversion which enables this but I would like to understand this a little
> > better.
> >
> > Can anyone help?
> >
> > Thanks in advance,
> >
> >
> >
> >
> >

  Reply With Quote
Old 26-06-2008, 04:33 PM   #4
Leo Tohill
Guest
 
Posts: n/a
RE: Question on inserting varbinary to SQL table via Powershell

ExecuteReader() is intended to execute a Select statement that returns a
result set (rows of data). The reader object returned by ExecuteReader() is
used to iterate through the result set. ExecuteNonQuery() is used to execute
statements that don't return data, such as Insert, Update, Delete, as well as
Create, Alter, Drop, etc.

(See also ExecuteScalar() for statements that return a single value.)



"Frank" wrote:

> I didn't know there was ExecuteNonQuery(). What is the difference?
>
> Thanks!
>
>
>
> "Leo Tohill" wrote:
>
> > Hello Frank,
> >
> > $cmd.CommandText="insert into binarytable (binary,datetime,type,service)
> > values(@FileData,'$datetime','$type','$service');"
> >
> > builds a string literal as the insert statement. Binary values simply
> > cannot be represented in a string literal. Using a parameter, as you have
> > done, is the most common approach to inserting binary data. Another
> > approach is hex-encoding the data. If you hex-encoded the data with the
> > right escape characters for sql server, then you could place it as a literal
> > into the insert, but
> > why bother?
> >
> > I'm puzzled why you use ExecuteReader() instead of ExecuteNonQuery().
> >
> > "Frank" wrote:
> >
> > > Hi,
> > >
> > > I apologize if this is the wrong discussion group but its a question of how
> > > to insert a varbinary (word doc) into SQL. I actually got this to work but
> > > need to understand why it works one way and not the other. Here is the
> > > procedure which works:
> > > ==============================================
> > > [byte[]]$file = Get-Content d:\test.docx -Encoding byte
> > > $DB = "server=sqlserver01;integrated security=sspi;database=test"
> > >
> > > $conn = new-object System.Data.SqlClient.SqlConnection
> > > $conn.ConnectionString = $DB
> > > $conn.Open()
> > > $cmd = new-object System.Data.SqlClient.SqlCommand
> > > $cmd.Connection=$conn
> > > $cmd.CommandText="insert into binarytable (binary,datetime,type,service)
> > > values(@FileData,'$datetime','$type','$service');"
> > > $cmd.Parameters.Add("@FileData", $file)
> > > $rdr = $cmd.ExecuteReader() | out-null 2>&1
> > > ====================================
> > >
> > > What doesn't work is using an insert statement and use the $file variable I
> > > set earlier, ie:
> > >
> > > $cmd.CommandText="insert into binarytable (binary,datetime,type,service)
> > > values('$file','$datetime','$type','$service');"
> > >
> > > The "$cmd.Parameters.Add("@FileData", $file)" line must do some sort of
> > > conversion which enables this but I would like to understand this a little
> > > better.
> > >
> > > Can anyone help?
> > >
> > > Thanks in advance,
> > >
> > >
> > >
> > >
> > >

  Reply With Quote
Reply

Thread Tools
Display Modes



< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 12:57 AM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional