Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
1.4 kB
1
Indexable
Never
using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                string commandText = @"INSERT INTO FileAttachment(ReferenceID, FileData, OriginalFileName,FileType,RefChildId) Values (@ReferenceID,@FileData,@OriginalFileName,@FileType,@RefChildId)";
                cmd.CommandText = commandText;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@ReferenceID", SqlDbType.Int);
                cmd.Parameters["@ReferenceID"].Value = item.ReferenceID;

                cmd.Parameters.Add("@OriginalFileName", SqlDbType.VarChar);
                cmd.Parameters["@OriginalFileName"].Value = item.OriginalFileName;

                cmd.Parameters.Add("@FileData", SqlDbType.VarBinary);
                cmd.Parameters["@FileData"].Value = item.FileAsByteArray;

                cmd.Parameters.Add("@FileType", SqlDbType.Int);
                cmd.Parameters["@FileType"].Value =(int)item.FileType;

                cmd.Parameters.Add("@RefChildId", SqlDbType.Int);
                cmd.Parameters["@RefChildId"].Value = item.RefchildID == null ? 0 : item.RefchildID;

                cmd.ExecuteNonQuery();
                cmd.Dispose();
              
                connection.Close();
            }