SQL语句中如何把文件以二进制数组形式存入数据库

时间:2022-06-05 01:39:53
System.IO.FileStream fs = new FileStream("1.txt", FileMode.Open);
string strConn = "Data Source=MOFEI-PC;Initial Catalog=Test;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
int v = ;
string sql = @"INSERT INTO [Test].[dbo].[TestB]([ID] ,[B]) VALUES(" + v + " ,@b)";
using (conn)
{
int fslength = (int)fs.Length;
byte[] Data = new Byte[fslength];
fs.Read(Data, , fslength);
SqlParameter prm1 = new SqlParameter("@b", SqlDbType.VarBinary, fslength, ParameterDirection.Input, true, , , " ", DataRowVersion.Current, Data);
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add(prm1);
int r1 = cmd.ExecuteNonQuery();
}
}