Array
(
)

Inserir e mostrar imagem SQL SERVER 2005

Hnkarla
   - 11 mar 2008

#Código

 protected void Button2_Click(object sender, EventArgs e)
    {
        {
            SqlConnection con = new SqlConnection("Data Source=srv-002;Initial Catalog=TesteWebService;User Id=User;Password=Senha");
            SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
            SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
            DataSet ds = new DataSet("MyImages");

            byte[] MyData = new byte[0];

            da.Fill(ds, "MyImages");
            DataRow myRow;
            myRow = ds.Tables["MyImages"].Rows[0];

            MyData = (byte[])myRow["ImgField"];
            int ArraySize = new int();
            ArraySize = MyData.GetUpperBound(0);

            FileStream fs = new FileStream(@"C:\Web
protected void Button2_Click(object sender, EventArgs e)
{
{
SqlConnection con = new SqlConnection("Data Source=srv-002;Initial Catalog=TesteWebService;User Id=User;Password=Senha");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");

byte[] MyData = new byte[0];

da.Fill(ds, "MyImages");
DataRow myRow;
myRow = ds.Tables["MyImages"].Rows[0];

MyData = (byte[])myRow["ImgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);

FileStream fs = new FileStream(@"C:\Web\1.jpg", FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Write(MyData, 0, ArraySize);

GridView.DataSource = ds;
GridView.DataBind();

fs.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
{
SqlConnection con = new SqlConnection("Data Source=srv-002;Initial Catalog=TesteWebService;User Id=User;Password=Senha");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");

da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
FileStream fs = new FileStream(@"C:\Web\1.jpg", FileMode.OpenOrCreate, FileAccess.Read);

byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

fs.Close();

da.Fill(ds, "MyImages");

DataRow myRow;
myRow = ds.Tables["MyImages"].NewRow();

myRow["Description"] = "This would be description text";
myRow["ImgField"] = MyData;
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");

con.Close();

}
.jpg", FileMode.OpenOrCreate, FileAccess.ReadWrite); fs.Write(MyData, 0, ArraySize); GridView.DataSource = ds; GridView.DataBind(); fs.Close(); } } protected void Button1_Click(object sender, EventArgs e) { { SqlConnection con = new SqlConnection("Data Source=srv-002;Initial Catalog=TesteWebService;User Id=User;Password=Senha"); SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con); SqlCommandBuilder MyCB = new SqlCommandBuilder(da); DataSet ds = new DataSet("MyImages"); da.MissingSchemaAction = MissingSchemaAction.AddWithKey; FileStream fs = new FileStream(@"C:\Web
protected void Button2_Click(object sender, EventArgs e)
{
{
SqlConnection con = new SqlConnection("Data Source=srv-002;Initial Catalog=TesteWebService;User Id=User;Password=Senha");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");

byte[] MyData = new byte[0];

da.Fill(ds, "MyImages");
DataRow myRow;
myRow = ds.Tables["MyImages"].Rows[0];

MyData = (byte[])myRow["ImgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);

FileStream fs = new FileStream(@"C:\Web\1.jpg", FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Write(MyData, 0, ArraySize);

GridView.DataSource = ds;
GridView.DataBind();

fs.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
{
SqlConnection con = new SqlConnection("Data Source=srv-002;Initial Catalog=TesteWebService;User Id=User;Password=Senha");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");

da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
FileStream fs = new FileStream(@"C:\Web\1.jpg", FileMode.OpenOrCreate, FileAccess.Read);

byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

fs.Close();

da.Fill(ds, "MyImages");

DataRow myRow;
myRow = ds.Tables["MyImages"].NewRow();

myRow["Description"] = "This would be description text";
myRow["ImgField"] = MyData;
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");

con.Close();

}
.jpg", FileMode.OpenOrCreate, FileAccess.Read); byte[] MyData = new byte[fs.Length]; fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length)); fs.Close(); da.Fill(ds, "MyImages"); DataRow myRow; myRow = ds.Tables["MyImages"].NewRow(); myRow["Description"] = "This would be description text"; myRow["ImgField"] = MyData; ds.Tables["MyImages"].Rows.Add(myRow); da.Update(ds, "MyImages"); con.Close(); }


To tentando fazer um grdview, onde aparece os dados de um produto e a sua imagem, mas a imagem não tá aparecendo, tentei colocar um novo campo no gridView , o campo ImageField, e não aparece msm! fica a imagem com aquele X cm se não existisse.. Alguem poderia ajudar??

Obrigada

Ivan.c#
   - 11 mar 2008

por que vc não faz o upload da foto pra uma pasta e guarda apenas o nome da mesma no BD.