473,782 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing BLOBs (through C#)

Following up on a question somebody asked a day-or-so ago...

When /reading/ BLOBs in C# I can use sequential access and GetBytes(); fine,
sorted.

However, is there a similar equivalent for /writing/ BLOBs? *Without* having
to buffer the entire thing into a byte[] to set into param.Value? Or would
the stream-friendly way of doing this be to execute multiple commands with
the UPDATE column_Name .WRITE (@chunk, NULL, 0) syntax [or UPDATETEXT]?

Thoughts?

Marc

Jun 1 '06 #1
1 1798
For ref, after some playing the best I can come up with is as follows; any
better offers?

/* Corresponding SP:

ALTER PROC mgtsave @id int, @data image, @append bit = 1
AS
DECLARE @ptr binary(16)
IF @append = 0 -- need to put in some empty data (not null) for
TEXTPTR to work
UPDATE MGT
SET data = ''
WHERE id = @id

SELECT @ptr = TEXTPTR(data)
FROM MGT
WHERE id = @id

IF @append = 1
UPDATETEXT MGT.data @ptr NULL 0 @data
ELSE
WRITETEXT MGT.data @ptr @data

*/
static void Main() {

long totalBytes = 0;
using (FileStream input = File.OpenRead(@ "C:\Out.pdf ")) //
random file
using (SqlConnection conn = new
SqlConnection(P roperties.Setti ngs.Default.Con Key))
using (SqlCommand cmd = conn.CreateComm and()) {
cmd.Parameters. Add("@id", System.Data.Sql DbType.Int).Val ue =
1; // just a row marker
SqlParameter paramData = cmd.Parameters. Add("@data",
System.Data.Sql DbType.Image); // the binary
SqlParameter paramAppend = cmd.Parameters. Add("@append",
System.Data.Sql DbType.Bit); // replace or append?
paramAppend.Val ue = false; // first pass should replace
existing
cmd.CommandText = "mgtsave";
cmd.CommandType = System.Data.Com mandType.Stored Procedure;
cmd.Prepare();
const int SQL_IMAGE_BUFFE R_SIZE = 8040; // optimal chunk
size
byte[] buffer = new byte[SQL_IMAGE_BUFFE R_SIZE];
int bytesRead;
conn.Open();
while((bytesRea d = input.Read(buff er, 0,
SQL_IMAGE_BUFFE R_SIZE)) > 0) {
if(bytesRead==S QL_IMAGE_BUFFER _SIZE) { // pass the
filled buffer
paramData.Value = buffer;
} else { // didn't fill an entire buffer
byte[] smallBuffer = new byte[bytesRead];
Buffer.BlockCop y(buffer, 0, smallBuffer, 0,
bytesRead);
paramData.Value = smallBuffer;
}
cmd.ExecuteNonQ uery();
paramAppend.Val ue = true; // subsequent calls should
append data
totalBytes += bytesRead;
}
conn.Close();
input.Close();
}
Console.WriteLi ne(totalBytes);
Console.ReadLin e();

}
Jun 1 '06 #2

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

Similar topics

1
3362
by: Kirby Urner | last post by:
I've been testing the Cookbook example 8.6 (2002 edition) re using cPickle to insert and retrieve BLOBs from mySQL, using Python's MySQLdb module. When I try to cPickle.loads(blob), I get an error telling me that loads wants a string, not type array.array. So I go cPickle.loads(blob.tostring()) instead and it works. My question is: has something changed in the Python API since this example was written?
0
2894
by: Ole Hansen | last post by:
Hi, Is it at all possible to insert BLOBs using the Array Interface? Today I have an application using the array interface. It works fine but so far I haven't been using BLOBs. I insert 100-200 rows in one server round trip. Now I want to have one or more colums of type BLOB but I cant see how this can fit into my current application.
6
1579
by: Marcus | last post by:
Hi all, Quick question... I am storing jpegs as blobs in a mysql table, which is fine on my live server because I am just uploading them using phpmyadmin (I am working with a small number of images that will rarely change and thus do not have a need for an upload script). My problem is I do all my testing on my local host where I do not have phpmyadmin, I do everything with mysql through the command prompt. How can I insert rows of...
7
6950
by: Howard Lowndes | last post by:
My situation is that I am interacting PHP 4.1.2 to PostgreSQL 7.2.2 I have no difficulty inserting and managing BLOBs into the Large Object system table, and I have a user table called images which maintains the relationship between the BLOB loid and the identity that relates to it in my user tables. So far so good. When I RTFM obout psql it refers to the \lo_import, \lo_list, \lo_export and \lo_unlink functions.
7
4055
by: Nilabhra Banerjee | last post by:
Hi, I am still not sure whether the BLOBS are actually stored in the database or they have the pointer to the database for that file in the filesystem. If I remove the files (sources) for BLOBS from the directories with the BLOB still hold the data ? Also one more very intriguing part is that if BLOBS are not deleted if we delete them from tables how to
0
1469
by: Bing | last post by:
Hi there, I am using the DB2 universal JDBC driver type 4 to insert BLOBs into a DB2 database. The Method I used for supplying the BLOB data value is setBinaryStream(). Everything works fine as long as the binary data is less than or equal to 100k. Anything larger than that (even for one byte) will result an exception saying the value is too large. All the DB2 samples (at least two)I have found have a limit of 100k (magic number!!!),...
1
2584
by: Kevin | last post by:
Hello - Quick question - does anyone know of a good way to archive a blob? Some background - Our application (Java Web Application) uses a DB2 table with a blob column of length 75000 to store some data. The data in this table needs to be in the system for approximately 60 days and then we want to archive the data "somewhere else" for legal purposes. We have put together a archive process using some Cobol programs on the mainframe to...
1
1764
by: mb345345 | last post by:
Hi group, I have a nice little CMS application which has been running for quite some time storing content in blobs in sql (the 'image' datatype) and spitting them out to a frame in the browser via a page called showContent.aspx: Response.ContentType = strContentType Response.BinaryWrite(objFileObject) However, Excel seems to display some rather odd behaviour:
2
2820
by: Jerry LeVan | last post by:
Hi, I am just getting into large objects and bytea "stuff". I created a small db called pictures and loaded some large objects and then tried to do a restore. Here is how I got the dump. pg_dump -Fc -b pictures > /Users/jerry/desktop/db.comp
0
9641
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.