SqlCommand.Prepare()不起作用

时间:2014-04-07 11:09:27

标签: c# sql parameters prepare

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

我已经在这里阅读了一些帖子,但我无法按照我需要的方式使用它。

  

例外:[System.InvalidOperationException] = {“The   SqlCommand.Prepare-Method需要,即所有变量   长度参数不是Null-Size。“}

代码:

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;
                }
            }
        }

提前致谢

放松

0 个答案:

没有答案