PDA

View Full Version : خواندن عکس از دیتابیس در دیتاگرید و پیکچرباکس



h.rezaee
پنج شنبه 01 تیر 1391, 15:34 عصر
سلام بر اساتید و دوستان حرفه ای.
یک سوال داشتم . ببینید من این کدها رو برای برنامه قرار دادم منتها فقط عکس رو از دیتابیس تو دیتاگرید میتونم بخونم وقتی میخوام اون رو تو پیکچرباکس قرار بدم اروور میده

//const string
private const string _commandtext = "SELECT an_name.*, txt.com, pic.datepic from an_name, txt, pic where an_name.id = txt.id and an_name.id = pic.id";

private const string _connectionstring = "Data Source=localhost;Initial Catalog=earth-planet-notronsoft;Integrated Security=True";

// private const string id_txt = "Select id From an_name Where id = " & ID

SqlConnection objconnection;
SqlCommand objcommand;
SqlDataAdapter objdataadapter;
DataSet objdataset;
DataView objdataview;
CurrencyManager objcurrencymanager;


public Form1()
{
objconnection = new SqlConnection(_connectionstring);
objcommand = new SqlCommand(_commandtext);
objdataadapter = new SqlDataAdapter(_commandtext, objconnection);
InitializeComponent();
}

private void filldatasetandview()
{
objdataset = new DataSet();
objdataadapter.Fill(objdataset, "an_name");
objdataview = new DataView(objdataset.Tables["an_name"]);
objdataadapter.Fill(objdataset, "txt");
objdataview = new DataView(objdataset.Tables["txt"]);


objcurrencymanager = (CurrencyManager)
(this.BindingContext[objdataview]);
}

private void bindfields()
{
txtlastname.DataBindings.Clear();
txtfirstname.DataBindings.Clear();
txt.DataBindings.Clear();
txtlastname.DataBindings.Add("Text", objdataview, "id");
txtfirstname.DataBindings.Add("Text", objdataview, "name");
txt.DataBindings.Add("Text", objdataview, "com");



DataTable d = new DataTable();
objdataadapter.Fill(d);
da1.DataSource = d;

toolStripStatusLabel1.Text = "Ready";
}

private void showposition()
{

txtreadposition.Text = (objcurrencymanager.Position + 1) +
" of " + objcurrencymanager.Count;
}

private void Form1_Load(object sender, EventArgs e)
{

cbofield.Items.Add("name");
cbofield.SelectedIndex = 0;
filldatasetandview();
bindfields();
showposition();
}



private void btnperformsearch_Click(object sender, EventArgs e)
{




int intposition;
switch (cbofield.SelectedIndex)
{
case 0:
objdataview.Sort = "name";
break;
}
if (cbofield.SelectedIndex < 1)
{
intposition = objdataview.Find(txtsearchcriteri.Text);
}
else
{
intposition = objdataview.Find(Decimal.Parse(txtsearchcriteri.Te xt));
}

if (intposition == -1)
{
toolStripStatusLabel1.Text = "Record Not Found";
}
else
{
toolStripStatusLabel1.Text = "Record Found";
objcurrencymanager.Position = intposition;
}
showposition();
}




حالا که کد زیر رو برای مشاهده عکس تو پیکچرباکس میزنم دیگه دیتاگرید نمیتونه اطلاعات رو نشون بده

SqlDataReader re;
re = objcommand.ExecuteReader();

byte[] Pic;
MemoryStream MS;
Pic = (byte[])re.GetValue(4);
MS = new MemoryStream();
MS.Write(Pic, 0, Pic.Length);
pictureBox1.BackgroundImage = Image.FromStream(MS);

objdataadapter.Fill(objdataset, "pic");
objdataview = new DataView(objdataset.Tables["pic"]);

h.rezaee
پنج شنبه 01 تیر 1391, 16:21 عصر
دوستان کمک کنید.

h.rezaee
پنج شنبه 01 تیر 1391, 16:58 عصر
دوستان مشکل قبلی رو حل کردم منتها الان این مشکل رو داره.
عکس تو پیکچر باکس نمایش داده میشه ولی فقط همون عکس اول دیتابیس رو نشون میده .
کدی هم که گذاشتم درست شد اینه.
try
{




objconnection.Open();
objdataadapter.Fill(objdataset, "id");
objconnection.Close();
byte[] arrPic = (byte[])(objdataset.Tables["id"].Rows[1]["datepic"]);
MemoryStream ms = new MemoryStream(arrPic);
pictureBox1.Image = Image.FromStream(ms);



}
catch (SystemException ex)
{
MessageBox.Show(ex.Message);
}