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
C# SqlCommand.Prepare()不起作用_C#_Sql_Parameters_Prepare - Fatal编程技术网

C# SqlCommand.Prepare()不起作用

C# SqlCommand.Prepare()不起作用,c#,sql,parameters,prepare,C#,Sql,Parameters,Prepare,我当前在为SqlCommand执行Prepare()语句时遇到问题。 我知道这是由于参数中没有随机值造成的,但我不知道如何处理,因为我一直在为这些参数设置新值 我已经在这里读了一些帖子,但是我不能按照我需要的方式使用它 异常:[System.InvalidOperationException]={“异常” SqlCommand.Prepare-Method要求所有变量 Lengthparameters的大小不为Null。“} 代码: 提前谢谢 放松您遇到了什么样的问题?在准备可变长度属性之前,您

我当前在为SqlCommand执行Prepare()语句时遇到问题。 我知道这是由于参数中没有随机值造成的,但我不知道如何处理,因为我一直在为这些参数设置新值

我已经在这里读了一些帖子,但是我不能按照我需要的方式使用它

异常:[System.InvalidOperationException]={“异常” SqlCommand.Prepare-Method要求所有变量 Lengthparameters的大小不为Null。“}

代码:

提前谢谢


放松

您遇到了什么样的问题?在准备可变长度属性之前,您需要预设最大大小。但是,当offset==0时,仍将覆盖准备好的查询。看看它,应该在循环之外,但至少应该使用另一个SQlCommandInstance@LasseV.Karlsen很抱歉没有告诉你。编辑了我下面的帖子。你能把你的例外信息翻译成英语吗?没关系@SonerGönül。将命令设置为insert查询,准备该查询,然后第一次执行循环将command.CommandText更改为update语句,永远不会工作。。。
using (System.Data.SqlClient.SqlCommand command = conn.CreateCommand())
        {
            command.CommandText = "INSERT INTO " + dbName + ".dbo.tempTable (blob) values (@data)";
            command.CommandTimeout = 900;

            SqlParameter dataParam = new SqlParameter("@data", SqlDbType.VarBinary);
            command.Parameters.Add(dataParam);

            SqlParameter offsetParam = new SqlParameter("@offset", SqlDbType.BigInt);
            command.Parameters.Add(offsetParam);

            SqlParameter lengthParam = new SqlParameter("@len", SqlDbType.BigInt);
            command.Parameters.Add(lengthParam); 

            using (FileStream fs = new FileStream(fileSource, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] buffer = new byte[8192];
                int read = 0;
                int offset = 0;

                while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    dataParam.Value = buffer;
                    offsetParam.Value = offset;
                    lengthParam.Value = read;

                    command.ExecuteNonQuery();

                    if (offset == 0)
                    {
                        command.CommandText = "UPDATE " + dbName + ".dbo.tempTable SET blob.WRITE(@data, @offset, @len)";

                        // Prepare for Query
                        command.Prepare();
                    }

                    offset += read;
                }
            }
        }