在C#中,如何实现上传图像的功能……
要求:(1)通过浏览图片,可以上传到数据库(2)调用已经上传到数据库的图像,可以展示在pictureBox控件上(3)数据库如何让存储图像文件,数据类型是什么……谢谢大家...
要求:(1)通过浏览图片,可以上传到数据库(2)调用已经上传到数据库的图像,可以展示在pictureBox控件上(3)数据库如何让存储图像文件,数据类型是什么……谢谢大家,数据库连接我会,只要求示例代码(谁有现成的,复制过来就行……谢谢啦)
展开
2014-01-02
展开全部
namespace BinDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.pictureBox1.ImageLocation))
{
MessageBox.Show("请选择要插入的图片!");
return;
} using (SqlConnection cn = new SqlConnection("Server=.;UID=sa;Pwd=;Database=northwind"))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO image_table(pic) VALUES (@pic)", cn))
{
cmd.Parameters.Add("@pic", SqlDbType.Image).Value = File.ReadAllBytes(this.pictureBox1.ImageLocation);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("图片插入成功!");
}
}
} private void button3_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Title = "请选择要插入的图片";
ofd.Filter = "Gif图片|*.gif|Jpg图片|*.jpg|BMP图片|*.bmp";
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
ofd.Multiselect = false;
if (ofd.ShowDialog(this) == DialogResult.OK)
{
this.pictureBox1.ImageLocation = ofd.FileName;
}
else
{
MessageBox.Show("你没有选择图片");
}
}
} private void button2_Click(object sender, EventArgs e)
{
//随机显示一张图片
using (SqlConnection cn = new SqlConnection("Server=LJ-Y5XD7QNIFAFJ\\SQL2005;UID=sa;Pwd=;Database=BookManageDB"))
{
using (SqlCommand cmd = new SqlCommand("SELECT bookImage FROM BookInfo", cn))
{
cn.Open();
this.pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])cmd.ExecuteScalar(), false));
cn.Close();
}
}
}
}
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.pictureBox1.ImageLocation))
{
MessageBox.Show("请选择要插入的图片!");
return;
} using (SqlConnection cn = new SqlConnection("Server=.;UID=sa;Pwd=;Database=northwind"))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO image_table(pic) VALUES (@pic)", cn))
{
cmd.Parameters.Add("@pic", SqlDbType.Image).Value = File.ReadAllBytes(this.pictureBox1.ImageLocation);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("图片插入成功!");
}
}
} private void button3_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Title = "请选择要插入的图片";
ofd.Filter = "Gif图片|*.gif|Jpg图片|*.jpg|BMP图片|*.bmp";
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
ofd.Multiselect = false;
if (ofd.ShowDialog(this) == DialogResult.OK)
{
this.pictureBox1.ImageLocation = ofd.FileName;
}
else
{
MessageBox.Show("你没有选择图片");
}
}
} private void button2_Click(object sender, EventArgs e)
{
//随机显示一张图片
using (SqlConnection cn = new SqlConnection("Server=LJ-Y5XD7QNIFAFJ\\SQL2005;UID=sa;Pwd=;Database=BookManageDB"))
{
using (SqlCommand cmd = new SqlCommand("SELECT bookImage FROM BookInfo", cn))
{
cn.Open();
this.pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])cmd.ExecuteScalar(), false));
cn.Close();
}
}
}
}
}
2014-01-02
展开全部
参考代码如下:string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf( "\\ "));
string fileEx = filepath.Substring(filepath.LastIndexOf( ". ")+1);
string serverpath = "C:/aa/upload/ " + filename;
string dbpath = "\\upload\\ " + filename;
if (fileEx == "jpg " || fileEx == "bmp " || fileEx == "gif ")
{
FileUpload1.PostedFile.SaveAs(serverpath);
Response.Write( "成功 ");
Image1.ImageUrl = @ "..\aa\upload\ " + filename;
}
else
{
Response.Write( "失败 ");
}
string sql = "insert into gc_carno(id,carno,model,carphoto) values(100, ' "+TextBox1.Text+ " ', ' "+TextBox2.Text+ " ', ' "+dbpath+ " ') ";
OracleConnection conn = sda.getoracleconnection();
conn.Open();
OracleCommand cmd = new OracleCommand(sql,conn);
int i = cmd.ExecuteNonQuery();
if (i == 1)
{
Response.Write( " <script language= 'javascript '> alert( '添加成功 ') </script> ");
}
else
{
Response.Write( " <script language= 'javascript '> alert( '添加失败 ') </script> ");
}
在数据库存储的是服务器图片的物理地址,数据类型是varchar型。
string filename = filepath.Substring(filepath.LastIndexOf( "\\ "));
string fileEx = filepath.Substring(filepath.LastIndexOf( ". ")+1);
string serverpath = "C:/aa/upload/ " + filename;
string dbpath = "\\upload\\ " + filename;
if (fileEx == "jpg " || fileEx == "bmp " || fileEx == "gif ")
{
FileUpload1.PostedFile.SaveAs(serverpath);
Response.Write( "成功 ");
Image1.ImageUrl = @ "..\aa\upload\ " + filename;
}
else
{
Response.Write( "失败 ");
}
string sql = "insert into gc_carno(id,carno,model,carphoto) values(100, ' "+TextBox1.Text+ " ', ' "+TextBox2.Text+ " ', ' "+dbpath+ " ') ";
OracleConnection conn = sda.getoracleconnection();
conn.Open();
OracleCommand cmd = new OracleCommand(sql,conn);
int i = cmd.ExecuteNonQuery();
if (i == 1)
{
Response.Write( " <script language= 'javascript '> alert( '添加成功 ') </script> ");
}
else
{
Response.Write( " <script language= 'javascript '> alert( '添加失败 ') </script> ");
}
在数据库存储的是服务器图片的物理地址,数据类型是varchar型。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-02
展开全部
学习了,一个上传到服务器地址serverpath ,一个数据库地址dbpath
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询