Stream ms;
byte[] dllbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
ofdSelectPic.DefaultExt = "压缩文件.zip|*.zip";
ofdSelectPic.Filter = "压缩文件.zip|*.zip";
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
string filepath = ofdSelectPic.FileName;
string extension = Path.GetExtension(filepath);
if (extension == ".zip")
{
dllbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(dllbyte, 0, Convert.ToInt32(ms.Length));
int count = updateSql(textBox1.Text, Convert.ToInt32(ms.Length),dllbyte);
if (count > 0)
{
this.Close();
MessageBox.Show("上传成功!");
}
else
MessageBox.Show("上传失败!");
ms.Close();
}
}
}
public int updateSql(string Version,int filesize,byte[] dll)
{
SqlConnection conn = new SqlConnection(connectionString);
string sqlstr = string.Format("update sys_verfile set version = '{0}',filesize='{1}',filebody=@img ", Version, filesize);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstr;
cmd.Connection = conn;
cmd.Parameters.Add("@img", System.Data.SqlDbType.Image);
cmd.Parameters[0].Value = dll;
conn.Open();
int result = Convert.ToInt32(cmd.ExecuteNonQuery());
return result;
}
PS:在写更新语句时 发现image类型字段 直接赋值为获取到的byte[]后 无法更新压缩包里的数据,但是利用SqlCommand, 声明参数类型为System.Data.SqlDbType.Image时 update后就入库了 ,以后再做了解。