C# 在SQLSERVER数据库中存取图片
来源:互联网 发布:api是什么java中 编辑:程序博客网 时间:2024/06/10 00:03
//放入数据库
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=sa;database=ac");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into xia (names) values (@i)", conn);
byte[] ib = new Byte[6000000];
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
fs.Read(ib, 0, 6000000);
cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);
cmd.Parameters["@i"].Value = ib; cmd.ExecuteNonQuery();
conn.Close();
}
//从数据库中读取
try
{
System.Data.SqlClient.SqlConnection conn = new SqlConnection(connstring);
conn.Open();
string sql = "select ImageNum from image where ImageName=@name";
SqlCommand comm = new SqlCommand(sql, conn);
string strname = listBox1.SelectedItem.ToString();
comm.Parameters.Add("@name", SqlDbType.NVarChar).Value = strname;
System.Data.SqlClient.SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = comm;
System.Data.DataSet dataset = new DataSet();
adapter.SelectCommand.ExecuteNonQuery();
adapter.Fill(dataset, "image");
byte[] b = (byte[])dataset.Tables[0].Rows[0]["ImageNum"];
if (b.Length > 0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
System.Drawing.Bitmap bitmap = new Bitmap(stream);
imageshow.Image = bitmap;
imageshow.Left = 700;
imageshow.Width = 300;
imageshow.Height = 300;
stream.Close();
}
conn.Close();
}
catch (Exception)
{
MessageBox.Show("你可能没选择图片,请选择");
}
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=sa;database=ac");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into xia (names) values (@i)", conn);
byte[] ib = new Byte[6000000];
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
fs.Read(ib, 0, 6000000);
cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);
cmd.Parameters["@i"].Value = ib; cmd.ExecuteNonQuery();
conn.Close();
}
//从数据库中读取
try
{
System.Data.SqlClient.SqlConnection conn = new SqlConnection(connstring);
conn.Open();
string sql = "select ImageNum from image where ImageName=@name";
SqlCommand comm = new SqlCommand(sql, conn);
string strname = listBox1.SelectedItem.ToString();
comm.Parameters.Add("@name", SqlDbType.NVarChar).Value = strname;
System.Data.SqlClient.SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = comm;
System.Data.DataSet dataset = new DataSet();
adapter.SelectCommand.ExecuteNonQuery();
adapter.Fill(dataset, "image");
byte[] b = (byte[])dataset.Tables[0].Rows[0]["ImageNum"];
if (b.Length > 0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
System.Drawing.Bitmap bitmap = new Bitmap(stream);
imageshow.Image = bitmap;
imageshow.Left = 700;
imageshow.Width = 300;
imageshow.Height = 300;
stream.Close();
}
conn.Close();
}
catch (Exception)
{
MessageBox.Show("你可能没选择图片,请选择");
}
- C# 在SQLSERVER数据库中存取图片
- C# 在SQLSERVER数据库中存取图片
- C# SQLserver数据库图片存取
- C#中将图片转化为byte[]在数据库中存取
- C#操作数据库中存取图片文件
- C#数据库存取图片
- C#在SQl中存取图片image
- 在VB中存取数据库中的图片
- VC/MFC实现在SQLSERVER中存取图片初探(1)
- 数据库中存取图片
- 数据库中存取图片
- 数据库中图片存取
- ASP.NET存取SQLServer数据库图片
- ASP.NET存取SQLServer数据库图片
- 利用C#从Access数据库中存取图片
- C#在SQL2005存取图片
- .Net在SqlServer中的图片存取技术
- .NET在SQLServer中的图片存取技术
- [转]用XmlReader 读取 Excel 2007 文件
- 初识struts
- ASP.NET中引用dll“找不到指定模块"的完美解决办法
- 开源领袖老黑客雷蒙德谈计算机语言
- Spring Acegi notes
- C# 在SQLSERVER数据库中存取图片
- 教你怎么缠网球拍羽毛球拍吸汗带
- New 关键词的三种用法 C#
- 重阳节的由来
- 求职初体会
- [转]DataGridView新特色(vs2005)
- SQL Server数据库的表中不能输入汉字的问题
- 使用WaveX API编写录音程序
- 认识茱萸,茱萸是什么