inserting images into access database
-
Hello I am creating a database which will be used to display information and pictures for houses. I have done everything but be able to insert the pictures and retrieve them from the database. This is the code l am using to make the connection with the database //Example Connection m_cnADONetConnection.ConnectionString = @"Provider=Microsoft.jet.OLEDB.4.0;data source=C:\Houses"; m_daDataAdapter = new OleDbDataAdapter("Select * From Houses",m_cnADONetConnection); OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daAdapter); m_daDataAdapeter.Fill(m_dtContacts); //This is what i am using to retrieve from the database, not sure how to do this for a image control. txtHouseID.text = Rows[rowPosition]["HouseID"].ToString(); To select the picture l want to add to the database, l am using a open dialog box, which works, but l can't add it to the database. Many thanks in advance Steve
-
Hello I am creating a database which will be used to display information and pictures for houses. I have done everything but be able to insert the pictures and retrieve them from the database. This is the code l am using to make the connection with the database //Example Connection m_cnADONetConnection.ConnectionString = @"Provider=Microsoft.jet.OLEDB.4.0;data source=C:\Houses"; m_daDataAdapter = new OleDbDataAdapter("Select * From Houses",m_cnADONetConnection); OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daAdapter); m_daDataAdapeter.Fill(m_dtContacts); //This is what i am using to retrieve from the database, not sure how to do this for a image control. txtHouseID.text = Rows[rowPosition]["HouseID"].ToString(); To select the picture l want to add to the database, l am using a open dialog box, which works, but l can't add it to the database. Many thanks in advance Steve
First, learn to use
OleDbParameter
. This will greatly aide in your problems. Second, set the column in which you want to store the image to an OLEObject. Then, you need to be able to read the image from aStream
as abyte[]
array and assign it:OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO SomeTable (Name, Image) " +
"VALUES (@Name, @Image)";
cmd.Parameters.Add("@Name", OleDbType.VarWChar, 40).Value = "ImageName";
cmd.Parameters.Add("@Image", OleDbType.Binary).Value = image;
cmd.ExecuteNonQuery();-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
First, learn to use
OleDbParameter
. This will greatly aide in your problems. Second, set the column in which you want to store the image to an OLEObject. Then, you need to be able to read the image from aStream
as abyte[]
array and assign it:OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO SomeTable (Name, Image) " +
"VALUES (@Name, @Image)";
cmd.Parameters.Add("@Name", OleDbType.VarWChar, 40).Value = "ImageName";
cmd.Parameters.Add("@Image", OleDbType.Binary).Value = image;
cmd.ExecuteNonQuery();-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Thanks for your help I was starting to read up on the stream. Could you give me a example how to read the image from a stream as a byte data. I am interested in using the parameters more, as it seems to be very useful. Is there any good tutorial on them. Many thanks in advance Steve
-
Thanks for your help I was starting to read up on the stream. Could you give me a example how to read the image from a stream as a byte data. I am interested in using the parameters more, as it seems to be very useful. Is there any good tutorial on them. Many thanks in advance Steve
There's the .NET Framework SDK documentation - just type
OleDbParameter
in the index. There's probably tutorials on this site and around the 'net, but they're not hard to use and the documentation offers plenty of information and examples.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Hello I am creating a database which will be used to display information and pictures for houses. I have done everything but be able to insert the pictures and retrieve them from the database. This is the code l am using to make the connection with the database //Example Connection m_cnADONetConnection.ConnectionString = @"Provider=Microsoft.jet.OLEDB.4.0;data source=C:\Houses"; m_daDataAdapter = new OleDbDataAdapter("Select * From Houses",m_cnADONetConnection); OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daAdapter); m_daDataAdapeter.Fill(m_dtContacts); //This is what i am using to retrieve from the database, not sure how to do this for a image control. txtHouseID.text = Rows[rowPosition]["HouseID"].ToString(); To select the picture l want to add to the database, l am using a open dialog box, which works, but l can't add it to the database. Many thanks in advance Steve
The OLE DB .NET Provider does not support named parameters for passing parameters to a SQL Statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example: SELECT * FROM Customers WHERE CustomerID = ? As a result, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter. Thank You Bo Hunter
-
Hello I am creating a database which will be used to display information and pictures for houses. I have done everything but be able to insert the pictures and retrieve them from the database. This is the code l am using to make the connection with the database //Example Connection m_cnADONetConnection.ConnectionString = @"Provider=Microsoft.jet.OLEDB.4.0;data source=C:\Houses"; m_daDataAdapter = new OleDbDataAdapter("Select * From Houses",m_cnADONetConnection); OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daAdapter); m_daDataAdapeter.Fill(m_dtContacts); //This is what i am using to retrieve from the database, not sure how to do this for a image control. txtHouseID.text = Rows[rowPosition]["HouseID"].ToString(); To select the picture l want to add to the database, l am using a open dialog box, which works, but l can't add it to the database. Many thanks in advance Steve