代码之家  ›  专栏  ›  技术社区  ›  Thunder

如何在不使用参数的情况下使用sql查询在SQLServer中创建intert映像

  •  0
  • Thunder  · 技术社区  · 15 年前

    我知道下面的代码插入了图像

    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();
    

    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();
    
    5 回复  |  直到 15 年前
        2
  •  1
  •   a_horse_with_no_name    15 年前

    可以使用十六进制表示法:

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

    将在表中插入一个包含两个字节(255和1)的blob

        3
  •  1
  •   Stijn de Witt    15 年前

    别这样!

        4
  •  0
  •   Thunder    15 年前

    这是我想要的did:第一次转换将图像转换成字节,然后将字节转换成sting并使用insert SQL插入sting,检索图像的步骤我收回了!

        //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 +")") ;
    
        5
  •  0
  •   Community Mohan Dere    8 年前

    我重新构思了我的问题,我想我有一个解决方案,请看一下 convert image to stream of characters .