Sql server and binary object ....

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

let's say I create a 'SELECT * FROM MyTable' command on a table containig
binary data, image for example.

and the I create a DataAdapter and a SqlCommandBuilder on this adapter.
will it update (send back) the pictures ?
how do I write a UPDATE/INSERT statement for binary object ?

any tips, advice, etc ?...

on an other thought is there a way to tell the command builder not to put
this object back to the database ?
 
Lloyd Dupont said:
let's say I create a 'SELECT * FROM MyTable' command on a table containig
binary data, image for example.

and the I create a DataAdapter and a SqlCommandBuilder on this adapter.
will it update (send back) the pictures ?
how do I write a UPDATE/INSERT statement for binary object ?

Like normal sql stataments with parameters of type VarBinary.
Example (bi is binary field in Sql server while im is Image field, b is
bit):
this.sqlInsertCommand1.CommandText = "INSERT INTO Tubo(B, bi, im) VALUES
(@B, @bi, @im); SELECT Id, B, bi, im FROM Tubo" +

" WHERE (Id = @@IDENTITY)";

this.sqlInsertCommand1.Connection = this.sqlConnection1;

this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@B", System.Data.SqlDbType.Bit, 1,
"B"));

this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@bi", System.Data.SqlDbType.VarBinary,
50, "bi"));

this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@im", System.Data.SqlDbType.VarBinary,
2147483647, "im"));

//

// sqlUpdateCommand1

//

this.sqlUpdateCommand1.CommandText = "UPDATE Tubo SET B = @B, bi = @bi, im =
@im WHERE (Id = @Original_Id) AND (B = @Or" +

"iginal_B) AND (bi = @Original_bi OR @Original_bi IS NULL AND bi IS NULL);
SELECT" +

" Id, B, bi, im FROM Tubo WHERE (Id = @Id)";

this.sqlUpdateCommand1.Connection = this.sqlConnection1;

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@B", System.Data.SqlDbType.Bit, 1,
"B"));

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@bi", System.Data.SqlDbType.VarBinary,
50, "bi"));

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@im", System.Data.SqlDbType.VarBinary,
2147483647, "im"));

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Id",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false,
((System.Byte)(0)), ((System.Byte)(0)), "Id",
System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_B", System.Data.SqlDbType.Bit,
1, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "B", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_bi",
System.Data.SqlDbType.VarBinary, 50, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "bi",
System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Id", System.Data.SqlDbType.Int, 4,
"Id"));


any tips, advice, etc ?...

Create stataments at design time. It is exepensive to build them at runtime,
and less flexible.
on an other thought is there a way to tell the command builder not to put
this object back to the database ?

Do it at design time.
 
¤ let's say I create a 'SELECT * FROM MyTable' command on a table containig
¤ binary data, image for example.
¤
¤ and the I create a DataAdapter and a SqlCommandBuilder on this adapter.
¤ will it update (send back) the pictures ?
¤ how do I write a UPDATE/INSERT statement for binary object ?
¤
¤ any tips, advice, etc ?...
¤
¤ on an other thought is there a way to tell the command builder not to put
¤ this object back to the database ?
¤

See the following:

HOW TO: Read and Write BLOB Data by Using ADO.NET with Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;308042&Product=adonet

HOW TO: Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;309158&Product=adonet


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Back
Top