请大家看看可否保存成功
private void button1_Click(object sender, EventArgs e)
{//浏览图片
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter="(*.jpg;*bmp)|*.jpg;*bmp|"+"(*.jpg)|*jpg";
if (openfile.ShowDialog() == DialogResult.OK)
{
strName = openfile.FileName;
this.pictureBox1.Image = Image.FromFile(strName);
int i = openfile.FileName.LastIndexOf("\\") + 1;
strName = strName.Substring(i);
}
}
private void btnSave_Click(object sender, EventArgs e)
{//保存图片
if (strName.Trim() != "")
{
FileStream fs = File.OpenRead(strName);
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
SqlConnection conn = new SqlConnection("Data Source=MICROSOF-26D41B;Initial Catalog=picture;User ID=sa;Password=123");
conn.Open();
SqlCommand insertCmd = new SqlCommand();
insertCmd = conn.CreateCommand();
insertCmd.CommandText = "insert into picture (pict,imageFile)Values(@pict,'" + this.strName.Trim().ToString() + "')";
insertCmd.Parameters.Add("@pict", SqlDbType.Image,16);
insertCmd.Parameters["@pict"].Value = b;
MessageBox.Show("保存成功!");
insertCmd.ExecuteNonQuery();
conn.Close();
}
else
{
MessageBox.Show("请选择要保存的图片","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}