简体   繁体   English

如何将 Byte[] 转换为数据以在 SQL Server 的 varbinary(max) 列中插入值?

[英]How convert a Byte[] to a data to insert a value in a varbinary(max) column in SQL Server?

I have a object with a byte[] property, and I would like to convert this value to the correct value to can insert it into the database using T-SQL.我有一个带有byte[]属性的对象,我想将此值转换为正确的值,以便可以使用 T-SQL 将其插入到数据库中。

But I don't know how I could convert the byte[] to the correct value for T-SQL for the insert.但我不知道如何将byte[]转换为插入的 T-SQL 的正确值。

Thanks.谢谢。

You want to convert to VarBinary . 您想要转换为VarBinary

See the following: 请参阅以下内容:

SQL Server Data Type Mappings SQL Server数据类型映射

simple example (setting command parameter) 简单示例(设置命令参数)

byte[] data;
command.Parameters.Add("@data", SqlDbType.VarBinary).Value = data;

T-SQL example of how to pass in varbinary 如何传入varbinary的T-SQL示例

CREATE PROCEDURE YourStoredProc
    @data varbinary(max)
AS
BEGIN
  -- your code
END

Create a Console Application project and try this code 创建一个控制台应用程序项目并尝试此代码

// Sample Class
public class MyClass
{
    public byte[] data;
}

// Main 
static void Main(string[] args)
{
    MyClass cls = new MyClass();
    using (SqlConnection cn = new SqlConnection("CONNECTION STRING"))
    {
        cn.Open();
        using (SqlCommand cmd = new SqlCommand("insert into MyTable values (@data)", cn))
        {
            cmd.Parameters.AddWithValue("@data", cls.data);
            cmd.ExecuteNonQuery();
        }
    }
}

Generating raw varbinary to insert to database (copy-paste case)生成原始 varbinary 以插入到数据库(复制粘贴案例)

string ToVarbinary(byte[] data)
    {
        var sb = new StringBuilder((data.Length * 2) + 2);
        sb.Append("0x");

        for (int i = 0; i < data.Length; i++)
        {
            sb.Append(data[i].ToString("X2"));
        }

        return sb.ToString();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将C#中的byte []作为字符串传递到SQL Server存储过程并转换为varbinary(MAX) - How to pass byte[] from C# as string to SQL Server stored procedure and convert into varbinary(MAX) SQL Server VARBINARY(max) 到 c# byte[] - SQL Server VARBINARY(max) to c# byte[] 如何在Excel Server 2008的varbinary(max)列中插入/检索Excel文件? - How do I insert/retrieve Excel files to varbinary(max) column in SQL Server 2008? 如何在 SQL Server 中存储和检索 varbinary(max) 列 - How to Store and Retrieve a varbinary(max) Column in SQL Server 在SQL Server的Varbinary(MAX)或Image数据类型中存储转换为字节数组的位图 - Store Bitmap converted to byte array in Varbinary(MAX) or Image data type of SQL Server SQL Server VARBINARY(最大) - SQL Server VARBINARY(MAX) 从SQL Server中检索varbinary(MAX)到C#中的byte [] - Retrieve varbinary(MAX) from SQL Server to byte[] in C# 在SQL Server中将bytearray保存到VarBinary列只插入一个字节 - saving bytearray to VarBinary column in SQL Server inserts only one byte 如何在CSharp中将二进制字符串转换回Byte。 SQL Server 2012 Varbinary创建的十六进制数字(8) - How to convert binary String back to Byte in CSharp. Hex number created by SQL Server 2012 Varbinary(8) 将.docx文件转换为二进制文件并插入varbinary(Max)时,如何解决“字符串或二进制数据将被截断”的问题? - How to fix “String or binary data would be truncated” when convert .docx file to binary and insert into varbinary(Max)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM