简体   繁体   English

如何使用c#将字符串存储在varbinary(max)列中

[英]How can I store a string in a varbinary(max) column using c#

How can I store a string in a varbinary(max) column? 如何将字符串存储在varbinary(max)列中?

I'm havig trouble in the convertion process i'm doing this: 我在转换过程中遇到麻烦我正在这样做:

    cmd.CommandText = "Insert into " + bdcombo.Text + ".dbo.nomes (id, nome) values (@id, @nome)";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = sqlcon;

    cmd.Parameters.Add("@nome", SqlDbType.VarBinary, 20).Value = Convert.ToSByte(textBox1.Text);

If you want to store a string, use [n]varchar(max). 如果要存储字符串,请使用[n] varchar(max)。

If you must use varbinary(max), then to get the bytes you must use an encoding, for example: 如果必须使用varbinary(max),那么要获取字节,必须使用编码,例如:

byte[] theBytes = Encoding.UTF8.GetBytes(theString);

and later: 然后:

string theString = Encoding.UTF8.GetString(theBytes);

(when reading) (阅读时)

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

相关问题 如何在varbinary(max)中存储两个smallint值? - How can I store two smallint values in a varbinary(max)? 如何将图像存储到varbinary(max)列? - How to store images to a varbinary(max) column? 如何将图像存储到数据库C#中/如何将Byte []转换为varbinary(max)C# - How to store image to database C# / how to convert Byte[] to varbinary(max) C# 如何通过C#代码将长字符串存储在varchar(MAX)列中? - How to store a long string in a varchar(MAX) column by C# code? SQL Server和C#-如何检查我要上传的varbinary(max)文件是否已存在于表中? - SQL Server & C# - How can I check if a varbinary(max) file i'm uploading already exists in a table? C#/SQL:从 DataGridView 到 Picturebox 的 Varbinary(max) 列 - C#/SQL: Varbinary(max) column to DataGridView to Picturebox 如何在 SQL Server 中存储和检索 varbinary(max) 列 - How to Store and Retrieve a varbinary(max) Column in SQL Server 在C#中将Varbinary(Max)字符串转换为图像格式 - Converting Varbinary(Max) String to Image Format in C# 如何将C#中的byte []作为字符串传递到SQL Server存储过程并转换为varbinary(MAX) - How to pass byte[] from C# as string to SQL Server stored procedure and convert into varbinary(MAX) 如何在C#中将SQL的varbinary读取为字符串? - How to read varbinary of SQL as string in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM