openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
SqlConnection con = new SqlConnection(conmmsy);
con.Open();
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;//文件路径
//创建文件流,path参数是文件路径
FileStream fs = new FileStream(path, FileMode.Open);
int streamLength = (int)fs.Length; //获取文件流的长度。
byte[] image = new byte[streamLength]; //声明字节数组,用于保存图片文件
fs.Read(image, 0, streamLength); //把图片文件转换成为字节数组保存
fs.Close();
//插入Sql语句,@img是Sql语句参数。
string sql = string.Format("insert imagetable values(@img)", image);
SqlCommand com = new SqlCommand(sql, con); //con是一个有效的连接对象
//为命令对象添加参数,注意参数的类型
com.Parameters.Add(new SqlParameter("img", SqlDbType.Binary, image.Length,
ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, image));
com.ExecuteNonQuery(); //执行
MessageBox.Show("插入成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "插入图片不成功");
}
finally
{
con.Close();
}
SqlConnection con = new SqlConnection(conmmsy);
con.Open();
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;//文件路径
//创建文件流,path参数是文件路径
FileStream fs = new FileStream(path, FileMode.Open);
int streamLength = (int)fs.Length; //获取文件流的长度。
byte[] image = new byte[streamLength]; //声明字节数组,用于保存图片文件
fs.Read(image, 0, streamLength); //把图片文件转换成为字节数组保存
fs.Close();
//插入Sql语句,@img是Sql语句参数。
string sql = string.Format("insert imagetable values(@img)", image);
SqlCommand com = new SqlCommand(sql, con); //con是一个有效的连接对象
//为命令对象添加参数,注意参数的类型
com.Parameters.Add(new SqlParameter("img", SqlDbType.Binary, image.Length,
ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, image));
com.ExecuteNonQuery(); //执行
MessageBox.Show("插入成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "插入图片不成功");
}
finally
{
con.Close();
}