欢迎光临

木子屋

标题: [C#] Sql Server2000图片存取技术

作者: dnawo

日期: 2009-09-15 11:37 AM

分类: Win编程

内容: 复制内容到剪贴板 程序代码//存储
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;Password=sa"))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "Insert INTO Images(Pic) Values(@Pic)";
cmd.CommandType = CommandType.Text;
//读取图片
byte[] b;
using (FileStream fs = new FileStream(@"F:\b1274.jpg", FileMode.Open))
{
b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
}
//添加参数
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter("@Pic", SqlDbType.Image)
};
parms[0].Value = b;
for (int i = 0; i < parms.Length; i++)
cmd.Parameters.Add(parms[i]);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
复制内容到剪贴板 程序代码//读取
private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;Password=sa"))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "Select Top 1 Pic FROM Images orDER BY ID DESC";
cmd.CommandType = CommandType.Text;
pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])cmd.ExecuteScalar()));
}
conn.Close();
}
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.281 ms

返回

标题: [C#] Sql Server2000图片存取技术

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.281 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.359 ms

返回