怎么把图片转换成二级制数据然后存到存储过程再存储到数据库里面去?
说主题别说别的,别说路径,现在是必须存储过程!说路径的菊爆!图片先转成二级制数据再放在存储过程里面再存到数据库!该怎么做,求例子。求解释。求教育。求代码……该用数组存二级...
说主题别说别的,别说路径,现在是必须存储过程!说路径的菊爆!
图片先转成二级制数据再放在存储过程里面再存到数据库!
该怎么做,求例子。求解释。求教育。
求代码……该用数组存二级制数据再放到存储过程还是直接用个string
.net C# !!!!无事请绕道!!!谢谢! 展开
图片先转成二级制数据再放在存储过程里面再存到数据库!
该怎么做,求例子。求解释。求教育。
求代码……该用数组存二级制数据再放到存储过程还是直接用个string
.net C# !!!!无事请绕道!!!谢谢! 展开
2个回答
展开全部
//搜寻图片,返回图像数据
private byte[] GetPhoto()
{
string fileName="";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "所有格式|*.*|位图文件(*.bmp)" +
"|*.bmp|JPEG(*.jpg)|*.jpg|TIFF(*.tif)|*.tif";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog1.FileName;
this.pictureBox1.Image = Image.FromFile(fileName);
}
FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
byte[] photo = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
return photo; //返回图像数据。
}
//将照片图像数据放入数组photo
byte[] photo = GetPhoto();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("存储过程名字", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@Photo", SqlDbType.Image, photo.Length).Value = photo;
connection.Open();
command.ExecuteNonQuery();
}
private byte[] GetPhoto()
{
string fileName="";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "所有格式|*.*|位图文件(*.bmp)" +
"|*.bmp|JPEG(*.jpg)|*.jpg|TIFF(*.tif)|*.tif";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog1.FileName;
this.pictureBox1.Image = Image.FromFile(fileName);
}
FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
byte[] photo = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
return photo; //返回图像数据。
}
//将照片图像数据放入数组photo
byte[] photo = GetPhoto();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("存储过程名字", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@Photo", SqlDbType.Image, photo.Length).Value = photo;
connection.Open();
command.ExecuteNonQuery();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询