Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 以varbinary(最大值)上传文件_C#_Sql_Asp.net_File Upload_Varbinarymax - Fatal编程技术网

C# 以varbinary(最大值)上传文件

C# 以varbinary(最大值)上传文件,c#,sql,asp.net,file-upload,varbinarymax,C#,Sql,Asp.net,File Upload,Varbinarymax,我有一个数据库表,其中有两个varbinary(max)类型的字段,用于存储文件 但是,我无法按预期上传文件。这个问题困扰了我很长时间,我不知道我能做些什么来解决它。出现一条错误消息: An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect synt

我有一个数据库表,其中有两个varbinary(max)类型的字段,用于存储文件

但是,我无法按预期上传文件。这个问题困扰了我很长时间,我不知道我能做些什么来解决它。出现一条错误消息:

An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Data.dll but was not handled in user code
Additional information: Incorrect syntax near the keyword 'title'.
在ASP.NET中实现三层体系结构是我必须的

数据访问层

业务逻辑层

表示层-代码隐藏

我尝试使用IntelliTrace进行调试,它显示了一条消息

ADO.NET:执行非查询“在提交(标题、幻灯片、代码)中插入值('My Water节约型项目'、'System.Byte[]'、'System.Byte[])”


我这样做对吗?我试图运行,但异常错误仍然存在 现在

string queryStr = "INSERT INTO Submission(title,slides,codes)" + "VALUES('"+
            _title + "', '" +
           "0x" + BitConverter.ToString(_slides).Replace("-", "")+ "', '" +
           "0x" + BitConverter.ToString(_codes).Replace("-", "")  + "')";

您的问题是类型转换。如果将值作为字符串插入(并且使用这些单引号),则需要插入十六进制值,并在其前面加上0x

这将帮助您: “0x”+位转换器.ToString(byteArray).替换(“-”,”)

“0x”+位转换器.ToString(\u幻灯片).替换(“-”,“)+”,“+”

不应将字节转换为字符串。相反,您希望使用参数化查询(以避免sql注入)并将这些字节数组直接插入数据库

public int SubmissionInsert(string title, byte[] slides, byte[] codes)
{
    int nofRow;
    string query = "INSERT  INTO Submission ( title, slides, codes )" +
                    "VALUES  ( @Title, @Slides, @Codes );";

    using (var con = new SqlConnection(_connStr))
    {
        con.Open();
        using (var cmd = new SqlCommand(query, con))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@Title", title);
            cmd.Parameters.AddWithValue("@Slides", slides);
            cmd.Parameters.AddWithValue("@Codes", codes);
            nofRow = cmd.ExecuteNonQuery();
        }
    }
    return nofRow;
}

是否SQL查询中的前两个字符串之间没有空格<代码>“插入提交内容(标题、幻灯片、代码)”+“值(”我在两个字符串之间包含了空格,但错误仍然存在。您还应该参数化SQL,否则您将面临注入攻击。此外,为了更好地帮助我们诊断您的问题,请在执行查询之前复制变量
queryStr
的当前值。您应该回答您的问题riesI遵循了您的方法并使用了参数化查询。但是,出现了另一个异常。System.Data.SqlClient.SqlException类型的异常出现在System.Data.dll中,但未在用户代码中处理。其他信息:关键字“title”附近的语法不正确。您可以在ssms中手动运行insert查询吗?例如,
insert提交(标题、幻灯片、代码)值(‘我的节水项目’,空,空)
 protected void btn_submit_Click(object sender, EventArgs e)
    {
        string input = "";
        byte[] slideArr = null, codeArr= null;

        string strTestFilePath, strTestFileName, strContentType;
        Int32 intFileSize, intFileLength;
        Stream strmStream;

        if (f_codes.HasFile)
        {
                strTestFilePath = f_codes.PostedFile.FileName;
                strTestFileName = Path.GetFileName(strTestFilePath);
                intFileSize = f_codes.PostedFile.ContentLength;
                strContentType = f_codes.PostedFile.ContentType;

                //Convert the source codes file to byte stream to save to database
                strmStream = f_codes.PostedFile.InputStream;
                intFileLength = (Int32)strmStream.Length;
                codeArr = new byte[intFileLength + 1];
                strmStream.Read(codeArr, 0, intFileLength);
                strmStream.Close();

        }

         if (f_slide.HasFile)
        {
                strTestFilePath = f_slide.PostedFile.FileName;
                strTestFileName = Path.GetFileName(strTestFilePath);
                intFileSize = f_slide.PostedFile.ContentLength;
                strContentType = f_slide.PostedFile.ContentType;

                strmStream = f_slide.PostedFile.InputStream;
                intFileLength = (Int32)strmStream.Length;
                slideArr = new byte[intFileLength + 1];
                strmStream.Read(slideArr, 0, intFileLength);
                strmStream.Close();
            }

         //Pass to BLL
         input = sub.submissionUpload(tb_title.Text,slideArr,codeArr);
         //Display error messages
         lbl_message.Text = input;
    }
string queryStr = "INSERT INTO Submission(title,slides,codes)" + "VALUES('"+
            _title + "', '" +
           "0x" + BitConverter.ToString(_slides).Replace("-", "")+ "', '" +
           "0x" + BitConverter.ToString(_codes).Replace("-", "")  + "')";
public int SubmissionInsert(string title, byte[] slides, byte[] codes)
{
    int nofRow;
    string query = "INSERT  INTO Submission ( title, slides, codes )" +
                    "VALUES  ( @Title, @Slides, @Codes );";

    using (var con = new SqlConnection(_connStr))
    {
        con.Open();
        using (var cmd = new SqlCommand(query, con))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@Title", title);
            cmd.Parameters.AddWithValue("@Slides", slides);
            cmd.Parameters.AddWithValue("@Codes", codes);
            nofRow = cmd.ExecuteNonQuery();
        }
    }
    return nofRow;
}
I also got the same error when I am uploading doc USING ADO.NET and Storedproc.

I am using stored proc to upload word file to the table's column type varbinary(max).
There are so many examples with insert query to insert document but my scenario was stored proc. I spent lot of time in figuring out the solution. 

Stored Proc:`Alter PROC [dbo].[usp_GMS_SaveEngagementDocument](
        @pDocumentID INT=0,
        @pEngagementID INT,
        @pDocumentName NVARCHAR(100),
        @pContentType NVARCHAR(100),
        @pDocumentType NVARCHAR(100),
        @pDocumentContent VARBINARY(max)=null,
       @pUserID INT)
AS
BEGIN
--INSERT
END`



SOLUTION:

    param = new SqlParameter();
                param.ParameterName = "@pDocumentContent";
                param.Direction = ParameterDirection.Input;
                param.Value = document.DocumentContent;
                param.DbType = DbType.Binary;
                param.SqlDbType = SqlDbType.Binary;
                paramList.Add(param);



Setting SQLDBType as Binary and DbType as Binary solved my problem In calling stored proc.

END