Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在sql数据库C中保存图像#_C#_Sql Server_Image_Bytearray_Sql Insert - Fatal编程技术网

C# 在sql数据库C中保存图像#

C# 在sql数据库C中保存图像#,c#,sql-server,image,bytearray,sql-insert,C#,Sql Server,Image,Bytearray,Sql Insert,当用户选择时,我想将图像保存在SQL数据库中。到目前为止,我已经输入了这个代码。这不会给我任何错误,但不会添加到数据库中。 我认为SQL语句有问题 有人能帮我吗 这是我的代码: public void addImages(string tag1,string tag2,string tag3,string status,string fileName) { try { byte[] image = null; FileStream fsstream

当用户选择时,我想将图像保存在SQL数据库中。到目前为止,我已经输入了这个代码。这不会给我任何错误,但不会添加到数据库中。 我认为SQL语句有问题

有人能帮我吗

这是我的代码:

public void addImages(string tag1,string tag2,string tag3,string status,string fileName)
{
    try
    {
        byte[] image = null;
        FileStream fsstream = new FileStream(fileName,FileMode.Open,FileAccess.Read);
        BinaryReader br = new BinaryReader(fsstream);
        image = br.ReadBytes((int)fsstream.Length);

        SqlCommand command = new SqlCommand("INSERT INTO [ImagesAndTags] (Images,Tags,Tag2,Tag3,Status) values (@IMG,'" + tag1 + "','" + tag2 + "','" + tag3 + "','" + status + "')", con);
        con.Open();
        command.Parameters.Add(new SqlParameter("@IMG",image));
        SqlDataReader reader = command.ExecuteReader();
        MessageBox.Show("Added Successfully!!!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        while (reader.Read()) { }
    }
    catch(Exception ex) { }
}

ExecuteReader
返回数据。就你而言,你不是。您只需尝试在数据库中插入一行即可。这就是为什么你需要使用

并像对
image
变量那样参数化其他插入值。还可用于处理数据库连接和命令

int insertedRowCount = command.ExecuteNonQuery();

if(insertedRowCount > 0)
   MessageBox.Show("Added Successfully!!!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

从存储在数据库中的数据中删除所有反斜杠、单倒逗号和双倒逗号


用一些常量字符串替换它,并在重新检索时将其转换为原始字符串。

Um,什么?如果你认为他必须对转义数据进行编码,那你就错了。首先,使用参数化查询可以为他解决这个问题。另一方面,他传递的是一个字节数组(不是字符串),因此此时不处理字符(我们不知道数据库的类型)。