Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello

I made Visual C# WindowsFormsApplication Project and next I added SQL database.

I have column with "image" type.

How can I save and open image (for example .jpg) using this field.

My image column: "zdjecie_dzialki"

http://img1.aukcjoner.pl/gallery/013940161/1_f.jpg[^]

Thanks for help!

My code to load data from database:

C#
System.Data.SqlServerCe.SqlCeConnection con;
System.Data.SqlServerCe.SqlCeDataAdapter da;

DataSet ds_nr_dzialki;
DataSet ds_wlasciciel_dzialki;
DataSet ds_pow_dzialki;

string zapytanie_nr_dzialki;
string zapytanie_wlasciciel_dzialki;
string zapytanie_pow_dzialki;

con = new System.Data.SqlServerCe.SqlCeConnection();
con.ConnectionString = "Data      Source=C:\\Users\\Marcin\\Desktop\\BazaDanychMapKatastralnych\\WindowsFormsApplication1\\BazaDan ychMapKatastralnych.sdf";
con.Open();
con.Close();



    zapytanie_nr_dzialki = textBox1.Text;
    ds_nr_dzialki = new DataSet();
    string sql = "SELECT * FROM tabela_dzialki WHERE nr_dzialki =" +                zapytanie_nr_dzialki;
    da = new System.Data.SqlServerCe.SqlCeDataAdapter(sql, con);
    da.Fill(ds_nr_dzialki, "Dzialki");

    DataRow dRow = ds_nr_dzialki.Tables["Dzialki"].Rows[0];
    textBox_nr_dzialki.Text = dRow.ItemArray.GetValue(0).ToString();
    textBox_wlasciciel_dzialki.Text = dRow.ItemArray.GetValue(1).ToString();
    textBox_pow_dzialki.Text = dRow.ItemArray.GetValue(2).ToString();
    textBox_adres_dzialki.Text = dRow.ItemArray.GetValue(3).ToString();
    textBox_skala_dzialki.Text = dRow.ItemArray.GetValue(4).ToString();
    textBox_oznwydz_dzialki.Text = dRow.ItemArray.GetValue(5).ToString();
    textBox_nrksiegi_dzialki.Text = dRow.ItemArray.GetValue(6).ToString();
    textBox_typksiegi_dzialki.Text = dRow.ItemArray.GetValue(7).ToString();
    textBox_nrprowksiege_dzialki.Text = dRow.ItemArray.GetValue(8).ToString();
    textBox_datazap_dzialki.Text = dRow.ItemArray.GetValue(9).ToString();
Posted

Please find a CodeProject article related to storing and retrieving images in sql database with c#.

C# Save and Load Image from Database[^]

It include sample code also.
 
Share this answer
 
v3
Here is another solution.

Please find copied a sample Database table which save images and retrieve it.
CREATE TABLE Employees
(
    Id int,
    Name varchar(50) not null,
    Photo varbinary(max) not null
)


INSERT INTO Employees (Id, Name, Photo)
SELECT 10, 'John', BulkColumn
FROM Openrowset( Bulk 'C:\photo.bmp', Single_Blob) as EmployeePicture
 
Share this answer
 
I used this solution:

http://tech.cybernet.lu/?p=21[^]

Now I have problem with
C#
byte[] imgByteData = Convert.ToByte(sdr.Item("Picture"));

http://img1.aukcjoner.pl/gallery/013943067/1_f.jpg[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900