Question

I am using this code to save a picture into an access database table:

byte[] fromPath = File.ReadAllBytes(Picture_Path);
byte[] fromPath2 = File.ReadAllBytes(BacksidePicture_Path);

con.Open();

string query = "Insert Into DML_Books_List (" + 
    "ID,ISNBORCode, Title, Donor, DocType, Edition, Author1, Author2, Author3, " + 
    "Author4, Translator, Publisher, Subject, USubject, Shelf, Cost, " + 
    "Language, Pages, Image, BImage, Description, Date) VALUES ('" + 
    "2" + "','" + ISNB_AddBook_Books_TXT.Text + "', '" + 
    Title_AddBook_Books_TXT.Text + 
    "', '" + Donor_AddBook_Books_TXT.Text + "', '" + 
    DocType_AddBook_Books_CBE.SelectedItem + "', '" + 
    Edition_AddBook_Books_TXT.Text + "', '" + 
    Author1_AddBook_Books_TXT.Text + "', '" + 
    Author2_AddBook_Books_TXT.Text + "', '" + 
    Author3_AddBook_Books_TXT.Text + "', '" + 
    Author4_AddBook_Books_TXT.Text + "', '" + 
    Translator_AddBook_Books_TXT.Text + "', '" + 
    Publisher_AddBook_Books_CBE.SelectedItem + "', '" + 
    Subject_AddBook_Books_CBE.SelectedItem + "', '" + 
    USubject_AddBook_Books_CBE.SelectedItem + "', '" + 
    Shelf_AddBook_Books_CBE.SelectedItem + "', '" + 
    Cost_AddBook_Books_TXT.Text + "', '" + 
    Language_AddBook_Books_CBE.SelectedItem + "', '" + 
    Pages_AddBook_Books_TXT.Text + "', '" + 
    @fromPath + "', '" + @fromPath2 + "', '" + 
    Description_AddBook_Books_MemoEdit.Text + "', '" + 
    Date_AddBook_Books_TXT.Text + "')";

OleDbCommand myCommand = new OleDbCommand();
myCommand.CommandText = query;
myCommand.Connection = con;
myCommand.ExecuteNonQuery();
con.Close();

But it has some problems.

Please Help Me solve this problem.

Thank you

Was it helpful?

Solution 2

use parameters like below code:

Assume you have two columns in your table called ID and Image. Now you going to insert data using SQL parameters

you need SQL statement like Insert Into DML_Books_List(ID, [Image]) values (@id, @image)

@id and @image are the given names for parameters. You can set the parameter values by parameter name.

var pic = File.ReadAllBytes(yourFileName);
using(OleDbConnection con = new OleDbConnection(constr))
using(OleDbCommand cmd = new OleDbCommand("Insert Into DML_Books_List(ID, [Image]) values (@id, @image)", con))
{
    con.Open();
    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
    cmd.Parameters.AddWithValue("@image", pic);
    cmd.ExecuteNonQuery();
}

OTHER TIPS

OleDb.OleDbConnection cn = new OleDb.OleDbConnection();
cn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + Application.StartupPath + "\\data.mdb";
cn.Open();

byte[] arrImage = null;
string strImage = null;
IO.MemoryStream myMs = new IO.MemoryStream();
//
if ((this.picPhoto.Image != null)) {
    this.picPhoto.Image.Save(myMs, this.picPhoto.Image.RawFormat);
    arrImage = myMs.GetBuffer;
    strImage = "?";
} else {
    arrImage = null;
    strImage = "NULL";
}

OleDb.OleDbCommand myCmd = new OleDb.OleDbCommand();
myCmd.Connection = cn;
myCmd.CommandText = "INSERT INTO tblstudent(stdid, [name], photo) " + " VALUES(" + this.txtID.Text + ",'" + this.txtName.Text + "'," + strImage + ")";
if (strImage == "?") {
    myCmd.Parameters.Add(strImage, OleDb.OleDbType.Binary).Value = arrImage;
}

Interaction.MsgBox("Data save successfully!");
myCmd.ExecuteNonQuery();
cn.Close();

Source

Use parametrized query..

OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database");
OleDbCommand command = connection.CreateCommand();

imageData = ReadByteArrayFromFile(@"c:\test.jpg");
command.CommandText = "Insert into SomeTable (Name, ImageData) VALUES (@Name, @Img)"
command.Parameters.AddWithValue("@Name", "theName");
command.Parameters.AddWithValue("@Img", imageData);
command.ExecuteNonQuery();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top