111,098
社区成员




protected int imageSize;
protected byte[] imageBody;
protected string SqlCom = "";
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
//创建数据库链接对象
con = new SqlConnection(ConfigurationManager.ConnectionStrings["databasecon"].ToString());
}
//图片上传
protected void BT_UpFile_Click(object sender, EventArgs e)
{
string imagePath;
string imageType;
string imageName;
if (UpFileControl.PostedFile.FileName != "")
{
//取得选择的图片路径
imagePath = UpFileControl.PostedFile.FileName;
//取得选择的图片扩展名
imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
//取得选择的图片名
imageName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1);
if ("jpg" != imageType && "gif" != imageType)
{
Response.Write("对不起!请您选择JPG或者GIF格式的图片!");
return;
}
else
{
//建立访问客户端上传文件的对象
HttpPostedFile SendImage = UpFileControl.PostedFile;
//取得图片的大小
imageSize = SendImage.ContentLength;
imageBody = new Byte[imageSize];
//建立数据流对象
Stream StreamObject = SendImage.InputStream;
//把图像数据放到imageBody中,其中0代表数据指针位置,ImageSize代表要读取的流的长度
StreamObject.Read(imageBody, 0, imageSize);
SqlCom = "insert into Images(ImageName,Images,ImageSize,imgtype)";
SqlCom += " VALUES('" + imageName + "','" + imageBody + "','" + imageSize.ToString() + "','" + imageType + "')";
con.Open();
cmd = new SqlCommand(SqlCom, con);
try
{
cmd.ExecuteNonQuery();
Response.Write("成功!");
}
catch
{
Response.Write("失败!");
}
finally
{
con.Close();
}
}
}
}
byte[] imagebody;
SqlCommand cmd = new SqlCommand("insert into tbname(col1,col2) values(@col1,@col2)");
cmd.Parameters.Add("@col1", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("@col2",SqlDbType.Image).Value=imagebody;
cmd.ExecuteNonQuery();