Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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#中的GetBytes未返回正确的值_C#_Sql Server_Varbinary - Fatal编程技术网

C#中的GetBytes未返回正确的值

C#中的GetBytes未返回正确的值,c#,sql-server,varbinary,C#,Sql Server,Varbinary,我有一个执行存储过程并返回该存储过程值的方法。我有一个VIN,在SQL中是VarBinary类型。我不确定我可以使用什么来获取值 这是我的代码: // Get Customer Product By CustomerID public Model.CustomerProduct Get_CustomerProduct(int Customer_ID) { Model.CustomerProduct model = null; string

我有一个执行存储过程并返回该存储过程值的方法。我有一个VIN,在SQL中是
VarBinary
类型。我不确定我可以使用什么来获取值

这是我的代码:

    // Get Customer Product By CustomerID
    public Model.CustomerProduct Get_CustomerProduct(int Customer_ID)
    {
        Model.CustomerProduct model = null;

        string myConnection = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ToString();
        SqlDatabase db = new SqlDatabase(myConnection);
        int bufferSize = 100;                   

        byte[] outByte = new byte[bufferSize];  
        using (DbCommand command = db.GetStoredProcCommand("Get_Customer_Product"))
        {
            db.AddInParameter(command, "Customer_ID", DbType.Int32, Customer_ID);

            var result = db.ExecuteReader(command);

            try
            {
                if (result.FieldCount == 0)
                    model = null;
                else
                {
                    result.Read();
                    model = new Model.CustomerProduct()
                    {
                       Product_Name = result.GetString(2)
                      ,VIN =result.GetBytes(3,0,outByte,0,bufferSize)   // this return me wrong 



                    };
                }
            }
            catch (Exception ex)
            {


            }
            return model;
        }
    }
我的问题是这一行:

     VIN =result.GetBytes(3,0,outByte,0,bufferSize) 
这将返回44,但假定返回的值为:

0x00D1CCE9771AE7554D479F7B93A456101000004158D130E5097EF2924DEC46255E5BAF4C8EF4C2AC2AA8FD9F29295F41DA3550123C6C4575788F5E6

该方法返回已写入数组的字节数,而不是字节本身。查看一下
outByte
的内容,您应该可以在那里找到您的数据

我还建议您首先使用空缓冲区调用
GetBytes
。这将导致它返回字段的长度,从而允许您正确调整缓冲区的大小:

int len = result.GetBytes( 3, 0, null, 0, 0 );
byte[] buf = new byte[len];
result.GetBytes( 3, 0, buf, 0, buf.Length );

model = new Model.CustomerProduct()
{
    Product_Name = result.GetString(2),
    VIN = buf
};
如果您现在运行的代码,您很可能需要将
VIN
的类型也更改为
byte[]

该方法返回写入数组的字节数,而不是字节本身。查看一下
outByte
的内容,您应该可以在那里找到您的数据

我还建议您首先使用空缓冲区调用
GetBytes
。这将导致它返回字段的长度,从而允许您正确调整缓冲区的大小:

int len = result.GetBytes( 3, 0, null, 0, 0 );
byte[] buf = new byte[len];
result.GetBytes( 3, 0, buf, 0, buf.Length );

model = new Model.CustomerProduct()
{
    Product_Name = result.GetString(2),
    VIN = buf
};

如果您现在运行的代码,您很可能需要将
VIN
的类型也更改为
byte[]

GetBytes返回读取的字节数。由于将Getbytes返回值分配给VIN,因此可以看到VIN中读取的字节数


相反,您必须从输出缓冲区读取,即在您的情况下,outByte将具有读取的字节。

GetBytes返回读取的字节数。由于将Getbytes返回值分配给VIN,因此可以看到VIN中读取的字节数


相反,您必须从输出缓冲区读取,即在您的情况下,outByte将读取字节。

谢谢,但我将获得所有00000000000000000000000000000000000000000000000000。db中的字段是什么类型的?db中的类型是VarBinary(500)请简单地尝试
(byte[])结果[3]
。谢谢,但我会得到所有00000000000000000000000000000000000000000000。数据库中的字段是什么类型的?数据库中的类型是VarBinary(500),请简单地尝试
(字节[])结果[3]