amir200h
چهارشنبه 01 خرداد 1392, 15:51 عصر
سلام به همه دوستان. من از کد زیر برای درج عکس در بانکم استفاده میکنم
private void button1_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] arraypic = ms.GetBuffer();
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);database=savepic;trusted_connection =yes";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "insert into test (id,pic) values (@id,@pic)";
com.Parameters.AddWithValue("@id", textBox1.Text);
com.Parameters.AddWithValue("@pic", arraypic);
con.Open();
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("ok");
}
ولی هنگامی که میخوام عکسو از بانک بخونم با کد زیر، با خطا مواجه میشه
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);database=savepic;trusted_connection =yes";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "select * from test where id=" + textBox1.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
DataSet ds = new DataSet();
da.Fill(ds);
byte[] arraypic = {Convert.ToByte(ds.Tables[0].Rows[1].ToString())};
MemoryStream ms = new MemoryStream(arraypic);
pictureBox2.Image = Image.FromStream(ms);
}
104524
private void button1_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] arraypic = ms.GetBuffer();
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);database=savepic;trusted_connection =yes";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "insert into test (id,pic) values (@id,@pic)";
com.Parameters.AddWithValue("@id", textBox1.Text);
com.Parameters.AddWithValue("@pic", arraypic);
con.Open();
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("ok");
}
ولی هنگامی که میخوام عکسو از بانک بخونم با کد زیر، با خطا مواجه میشه
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);database=savepic;trusted_connection =yes";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "select * from test where id=" + textBox1.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
DataSet ds = new DataSet();
da.Fill(ds);
byte[] arraypic = {Convert.ToByte(ds.Tables[0].Rows[1].ToString())};
MemoryStream ms = new MemoryStream(arraypic);
pictureBox2.Image = Image.FromStream(ms);
}
104524