将图像插入数据库错误 [英] Insert image to database error

查看:92
本文介绍了将图像插入数据库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像插入数据库,但是我收到错误附加信息:@images附近的语法错误。













< pre lang =c#>使用系统; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Data.SqlClient;
使用System.IO;
命名空间Carprogram
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection connection = new SqlConnection(Data Source = NAWAF; Initial Catalog = CAR; Integrated Security = True);
string imgloc =;
SqlCommand cmd;
private void linkLabel4_LinkClicked(object sender,LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter =png文件(* .png)| * .png | jpg文件(*。jpg)| * .jpg |所有文件(*。*)| *。*;
if(dialog.ShowDialog()== DialogResult.OK)
{
imgloc = dialog.FileName.ToString();
pictureBox1.ImageLocation = imgloc;

}
}


private void groupBox1_Enter(object sender,EventArgs e)
{

}

private void save_Click(object sender,EventArgs e)
{


byte [] images = null;
FileStream Streem = new FileStream(imgloc,FileMode.Open,FileAccess.Read);
BinaryReader brs = new BinaryReader(Streem);
images = brs.ReadBytes((int)Streem.Length);

SqlConnection con = new SqlConnection(Data Source = NAWAF; Initial Catalog = CAR; Integrated Security = True);
con.Open();
SqlCommand cmd = new SqlCommand(@INSERT INTO [CAR]。[dbo]。[vehicleinfo]
([modely]
,[make]
,[model]
,[颜色]
,[类型]
,[里程表]
,[vin]
,[车辆]
,[driverop]
,[部门]
,[img])

VALUES
('+ modely.Text +','+ make.Text +',' + model.Text +','+ color.Text +','+ type.Text +''+ odometer.Text +','+ vin.Text +','+ vehicle.Text +','+ driverop.Text +','+ department.Text +',@ images,con);


cmd.Parameters。添加(@ images,图像);
cmd.ExecuteNonQuery();

con.Close();









}


}
}





什么我试过了:



 cmd.Parameters。 add  new  SqlParameter   @ images ,images); 

解决方案

不要这样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood '   

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

完全有效的删除表格通讯和

   -   ' 

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期进行备份,不是吗?



你知道参数化查询是什么,所以使用它们......然后在你的SQL中添加一个结束括号声明...

I want to insert image to the database but I got error " Additional information: Incorrect syntax near '@images'. "






<pre lang="c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace Carprogram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection connection = new SqlConnection("Data Source=NAWAF;Initial Catalog=CAR;Integrated Security=True");
        string imgloc="";
        SqlCommand cmd;
         private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
          OpenFileDialog dialog = new OpenFileDialog ();
             dialog.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.*";
             if(dialog.ShowDialog()==DialogResult.OK)
             {
                 imgloc =dialog.FileName.ToString();
                 pictureBox1.ImageLocation=imgloc;

             }
        }


        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void save_Click(object sender, EventArgs e)
        {


            byte[] images = null;
            FileStream Streem = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
            BinaryReader brs = new BinaryReader(Streem);
            images = brs.ReadBytes((int)Streem.Length);

            SqlConnection con = new SqlConnection("Data Source=NAWAF;Initial Catalog=CAR;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand(@"INSERT INTO [CAR].[dbo].[vehicleinfo]
                ( [modely]
                 ,[make]
                 ,[model]
                 ,[Color]
                 ,[type]
                 ,[odometer]
                 ,[vin]
                 ,[vehicle]
                 ,[driverop]
                 ,[department]
                 ,[img])
                
                VALUES
                     ('" + modely.Text + "' ,'" + make.Text + "' , '" + model.Text + "' , '" + color.Text + "' , '" + type.Text + "''" + odometer.Text + "' ,'" + vin.Text + "' , '" + vehicle.Text + "' , '" + driverop.Text + "' , '" + department.Text + "',@images",con);


            cmd.Parameters.Add("@images", images);
            cmd.ExecuteNonQuery();

            con.Close();









        }

       
    }
}



What I have tried:

cmd.Parameters.add(new SqlParameter"@images",images);

解决方案

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

You know what a parameterized query is, so use them... and then add a closing bracket to your SQL statement...


这篇关于将图像插入数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆