views:

43

answers:

5

I am trying to insert image in my databse .However I dont want to use the parameters as my set up of coding pattern does not allow this.Is there a way out?

I know that following code inserts the image

byte[] imageData = ReadFile(txtImagePath.Text);
SqlConnection CN = new SqlConnection(txtConnectionString.Text);
string qry = "insert into ImagesStore (ImageData) values( @ImageData)";
SqlCommand SqlCom = new SqlCommand(qry, CN);
SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)imageData));
//Open connection and execute insert query.
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close();

But However, I would like to use some thing like this without the SQL Parameters

byte[] imageData = ReadFile(txtImagePath.Text);
SqlConnection CN = new SqlConnection(txtConnectionString.Text);
string qry = "insert into ImagesStore (ImageData) values(IMAGE DATA IN SOME FORM MAY BE 0101000101011001100 I dont know!)";
SqlCommand SqlCom = new SqlCommand(qry, CN);
//Open connection and execute insert query.
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close();
+1  A: 

I haven't used sqlserver, but can you use a blob where you can just insert any binary object regardless of format? I found this article which may be of some help: http://www.developer.com/net/asp/article.php/3761486/Working-with-Binary-Large-Objects-BLOBs-Using-SQL-Server-and-ADONET.htm

MaQleod
It uses parameter So not the answer !
Thunder
+1  A: 

You can use a hex notation:

INSERT INTO imagestore
(imagedata)
VALUES
(0xFF01);

would insert a blob with two bytes (255 and 1) into the table

a_horse_with_no_name
but How to convert image to bytes ... do you have any idea?
Thunder
You already have a byte[] array. You just need to convert each byte into its hex representation.
a_horse_with_no_name
If you insist on storing the image in the DB without parameters, the above answer seems best. Just do a 'Byte array to hex string' conversion and insert the resulting data. I wouldn't do this on multiple megabyte images though as the hex encoding makes it twice as big. For small icons it's probably acceptable.
Stijn de Witt
A: 

Here is what I did:First converted the image into bytes then bytes into sting and inserted the sting using insert SQL, to retrieve the image I took back the steps!

    //convert to stirng
    Bitmap bmp = new Bitmap(@"D:/bmp.bmp");
    MemoryStream mem = new MemoryStream();
    bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg );
    byte[] b = mem.ToArray();
    mem.Close();
    mem = null;
    System.Text.UnicodeEncoding a = new UnicodeEncoding();
    string s = System.Text.Encoding.Unicode .GetString(b);
    //test
    File.WriteAllText(@"D:/txt.txt", s);

    //convert back to image
    Image newImage;
    byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
    using (MemoryStream ms = new MemoryStream(bytes.Length))
    {
        ms.Write(bytes, 0, bytes.Length);
        newImage = Image.FromStream(ms);
        ms.Close();
    }
    //test 
    pictureBox1.Image = newImage;
    //It works!
    //So just fire the SQL
   con.Executenonquery("insert into ImageDb ('img') values (" + s +")") ;
Thunder
I think you are just lucky that it works.In fact there are definitely byte combinations that do not map to a valid Unicode character. There are approx. 100.000 Unicode characters that are encoded into bytes using either UTF-8, UTF-16 or UTF-32. If you are using UTF-8 (I can't tell from your code) then there are so called 'surrogate pairs' and not all byte combinations are valid. If you use UTF-32 there a 4 billion possible combinations which definitely don't all map to 100.000 characters.In short: Don't use this code as it will probably fail at some point and will be impossible to debug.
Stijn de Witt
@Stijin is correct ,It didnt work for some other images :(
Thunder
+1  A: 

Dont'!

Sorry for dodging your question, but why do you want to insert the image as a blob? In my experience it is almost never a good idea. Instead store the path to the image file on disk.

It could be that rour requirements leave you no other option than to store the image as a blob in the db, but I would seriously reconsider the requirements as storing binary (image) data is almost always a bad idea.

Stijn de Witt
I re-framed my question and I think I have got a solution plz Have a look here . http://stackoverflow.com/questions/3733026/how-to-convert-an-image-in-to-a-stream-of-characters
Thunder
A: 

I re-framed my question and I think I have got a solution plz Have a look at convert image to stream of characters .

Thunder