namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
string str="";
Encoding ed;
MemoryStream ms;
byte[] bt;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=123456");
FileStream fs = new FileStream(@"C:\Documents and Settings\student\My Documents\My Pictures\Ascent.jpg", FileMode.Open);
bt=new byte[fs.Length];
fs.Read(bt,0,bt.Length);
fs.Close();
MessageBox.Show(bt.Length.ToString());
try
{
conn.Open();
string sql = "insert into ImageInfo values(@a,@b)";
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.Parameters.Add("@a",SqlDbType.VarChar,20);
cmd.Parameters["@a"].Value = "Ascent.jpg";
cmd.Parameters.Add("@b",SqlDbType.Image,bt.Length);
cmd.Parameters["@b"].Value = bt;
int row=cmd.ExecuteNonQuery();
if (row >= 1)
{
MessageBox.Show("插入成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=123456");
try
{
conn.Open();
string sql = string.Format("select * from ImageInfo where id=1");
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
ms = new MemoryStream((byte[])sdr["content"]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
pictureBox1.Image = Image.FromStream(ms, true);
}
}
}
插入sql的转换语句
--INSERT INTO imageinfo SELECT * FROM OPENROWSET(BULK N'E:\项目\img\3.jpg', SINGLE_BLOB) as img
--SELECT * FROM OPENROWSET(BULK N'E:\项目\img\2.jpg', SINGLE_BLOB) AS a
{
public partial class Form2 : Form
{
string str="";
Encoding ed;
MemoryStream ms;
byte[] bt;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=123456");
FileStream fs = new FileStream(@"C:\Documents and Settings\student\My Documents\My Pictures\Ascent.jpg", FileMode.Open);
bt=new byte[fs.Length];
fs.Read(bt,0,bt.Length);
fs.Close();
MessageBox.Show(bt.Length.ToString());
try
{
conn.Open();
string sql = "insert into ImageInfo values(@a,@b)";
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.Parameters.Add("@a",SqlDbType.VarChar,20);
cmd.Parameters["@a"].Value = "Ascent.jpg";
cmd.Parameters.Add("@b",SqlDbType.Image,bt.Length);
cmd.Parameters["@b"].Value = bt;
int row=cmd.ExecuteNonQuery();
if (row >= 1)
{
MessageBox.Show("插入成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=123456");
try
{
conn.Open();
string sql = string.Format("select * from ImageInfo where id=1");
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
ms = new MemoryStream((byte[])sdr["content"]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
pictureBox1.Image = Image.FromStream(ms, true);
}
}
}
插入sql的转换语句
--INSERT INTO imageinfo SELECT * FROM OPENROWSET(BULK N'E:\项目\img\3.jpg', SINGLE_BLOB) as img
--SELECT * FROM OPENROWSET(BULK N'E:\项目\img\2.jpg', SINGLE_BLOB) AS a
本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1360707,如需转载请自行联系原作者