⑷ 在構造函數中添加代碼
string sqlstr="select * from pub_info";
conn=new SqlConnection(connString);
adapter=new SqlDataAdapter(sqlstr,conn);
SqlCommandBuilder builder=new SqlCommandBuilder(adapter);
adapter.UpdateCommand=builder.GetUpdateCommand();
dataset=new DataSet();
adapter.Fill(dataset,"pub_info");
//將text1Box1的Text屬性綁定到dataset中的pub_info表的pr_info字段
this.textBox1.DataBindings.Add(new Binding("Text",dataset,"pub_info.pr_info"));
for(int i=0;i<dataset.Tables[0].Rows.Count;i++)
{
this.listBox1.Items.Add(dataset.Tables[0].Rows[i][0]);
}
⑸ 添加調用的方法
private void ShowImage()
{
byte[] bytes= (byte[])dataset.Tables[0].Rows[this.listBox1.SelectedIndex][1];
MemoryStream memStream=new MemoryStream(bytes);
try
{
Bitmap myImage = new Bitmap(memStream);
this.pictureBox1.Image= myImage;
}
catch
{
this.pictureBox1.Image=null;
}
}
⑹ 添加“更換圖片”的Click事件代碼
private void buttonUpdateImage_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1=new OpenFileDialog();
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName.Trim()!="")
{
Stream myStream = openFileDialog1.OpenFile();
int length=(int)myStream.Length;
byte[] bytes=new byte[length];
myStream.Read(bytes,0,length);
myStream.Close();
dataset.Tables[0].Rows[this.listBox1.SelectedIndex][1] =bytes;
ShowImage();
}
}