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

时间:2011-11-24 10:29:07

标签: c# sql sql-server

如何将字符串存储在varbinary(max)列中?

我在转换过程中遇到了麻烦 我这样做:

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

1 个答案:

答案 0 :(得分:15)

如果要存储字符串,请使用[n] varchar(max)。

如果您必须使用varbinary(max),那么要获取字节,您必须使用编码,例如:

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

以后:

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

(阅读时)

相关问题