Insert binary data into SQL from c# without a stored procedure

后端 未结 3 1874
别跟我提以往
别跟我提以往 2021-01-13 05:03

Does anyone know whether if its possible to insert binary data into an SQL field from c# without using a stored procedure?

For example - convert a byte array into ba

3条回答
  •  野的像风
    2021-01-13 05:38

    Try this code, either the command.Parameters that is uncommented or the commented code should work. I use OracleDataClient at work, so I took this code almost completely from 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);
        }
    }
    

    edit: this assumes b is already byte[]. I just looked at some old code and update the parameter to what worked for me (SQL Server 2005)

提交回复
热议问题