Gra*_*ant 5 c# sql binary stored-procedures
有没有人知道是否可以在不使用存储过程的情况下从c#将二进制数据插入到SQL字段中?
例如 - 将字节数组转换为base64或类似的东西,然后使用如下文本命令...
String.Format("update A set B = {0} where C = D", Convert.ToBase64String(b));
Run Code Online (Sandbox Code Playgroud)
其中b是字节数组.
谢谢
当然,你应该使用参数化语句,但如果你真的需要,你可以这样做:
byte[] b = null; //(your input should be a byte[])
String.Format("update A set B = 0x{0} where C = D", BitConverter.ToString(b).Replace("-", "").ToLower());
Run Code Online (Sandbox Code Playgroud)
SQL Server期望十六进制格式的二进制数据没有连字符,小写字母的前缀为" 0x "
尝试此代码,取消注释的命令.参数或注释代码应该工作.我在工作中使用OracleDataClient,所以我几乎完全从MSDN中获取此代码
string commandText= "update A set B = @BIN where C = D";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("@BIN", SqlDbType.Binary, b.Length).Value = b;
// command.Parameters.AddWithValue("@BIN ", b);
try
{
connection.Open();
Int32 rowsAffected = command.ExecuteNonQuery();
Console.WriteLine("RowsAffected: {0}", rowsAffected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这假设b已经是byte [].我只是查看了一些旧代码并将参数更新为对我有用的参数(SQL Server 2005)
| 归档时间: |
|
| 查看次数: |
12505 次 |
| 最近记录: |