SQL Server2000
private void button1_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog ();
if (openFileDialog1.FileName.Trim()!="")
{
FileInfo fi = new FileInfo(openFileDialog1.FileName);
string imgtitle=openFileDialog1.FileName;
int imgdatalen=(int)fi.Length;
byte[] imgdata = new byte[imgdatalen];
Stream imgdatastream=fi.OpenRead();
int n=imgdatastream.Read(imgdata,0,imgdatalen);
if( conn.State == ConnectionState.Open)
conn.Close();
ConnectionString ="Integrated Security=SSPI;" + "Initial Catalog=mydb;" +"Data Source=localhost;";
conn.ConnectionString = ConnectionString;
try
{
string mySelectQuery = "INSERT INTO ImageStore(imgdata) VALUES (@imgdata )";
SqlCommand myCommand = new SqlCommand(mySelectQuery, conn);
SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.Value = imgdata;
myCommand.Parameters.Add( paramData );
conn.Open();
int numRowsAffected = myCommand.ExecuteNonQuery();
conn.Close();
MessageBox.Show("成功放入图片到数据库中");
}
catch(Exception err)
{
MessageBox.Show("您输入名称可能在数据库中已存在或输入为空,请检查!"+err.ToString() );
}
finally
{}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
if( conn.State == ConnectionState.Open)
conn.Close();
ConnectionString ="Integrated Security=SSPI;" + "Initial Catalog=mydb;" +"Data Source=localhost;";
conn.ConnectionString = ConnectionString;
string sql="SELECT * FROM ImageStore where ;
SqlCommand command = new SqlCommand(sql, conn);
try
{
conn.Open();
}
catch
{
MessageBox.Show(" 不能打开数据联接!") ;
}
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
byte[] mydata=((byte[])dr["imgdata"]);
MemoryStream myStream=new MemoryStream();
foreach(byte a in mydata)
{
myStream.WriteByte(a);
}
Image myImage=Image.FromStream(myStream);
myStream.Close();
pic1.Image=myImage;
pic1.Refresh();
}
else
{
MessageBox.Show("没有成功读入数据!") ;
}
conn.Close();
Access部分
private void button1_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog ();
if (openFileDialog1.FileName.Trim()!="")
{
FileInfo fi = new FileInfo(openFileDialog1.FileName);
string imgtitle=openFileDialog1.FileName;
int imgdatalen=(int)fi.Length;
byte[] imgdata = new byte[imgdatalen];
Stream imgdatastream=fi.OpenRead();
int n=imgdatastream.Read(imgdata,0,imgdatalen);
string strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"//mydb.mdb";
OleDbConnection conn = new OleDbConnection ( strCon ) ;
try
{
string mySelectQuery = "INSERT INTO ImageStore(imgdata) VALUES (@imgdata )";
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, conn);
OleDbParameter paramData = new OleDbParameter( "@imgdata", SqlDbType.Image );
paramData.Value = imgdata;
myCommand.Parameters.Add( paramData );
conn.Open();
int numRowsAffected = myCommand.ExecuteNonQuery();
conn.Close();
MessageBox.Show("成功放入图片到数据库中");
}
catch(Exception err)
{
MessageBox.Show("您输入名称可能在数据库中已存在或输入为空,请检查!"+err.ToString() );
}
finally
{}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
string strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"//mydb.mdb";
OleDbConnection conn = new OleDbConnection ( strCon ) ;
if(textBox1.Text.Trim()=="") MessageBox.Show("显示不能为空!");
else
{
string sql="SELECT * FROM ImageStore where ;
OleDbCommand command = new OleDbCommand(sql, conn);
try
{
conn.Open();
}
catch
{
MessageBox.Show(" 不能打开数据联接!") ;
}
OleDbDataReader dr = command.ExecuteReader();
if(dr.Read())
{
byte[] mydata=((byte[])dr["imgdata"]);
MemoryStream myStream=new MemoryStream();
foreach(byte a in mydata)
{
myStream.WriteByte(a);
}
Image myImage=Image.FromStream(myStream);
myStream.Close();
pic1.Image=myImage;
pic1.Refresh();
}
else
{
MessageBox.Show("没有成功读入数据!") ;
}
conn.Close();
}
上面内容为程序片段,如有不明白可以留言。