如何将图片保存到访问数据库表?

时间:2014-02-06 18:24:15

标签: c# database image ado.net send

我正在使用此代码将图片保存到访问数据库表中:

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();

但它有一些问题。

请帮我解决这个问题。

谢谢

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

使用以下代码参数:

假设您的表中有两列名为IDImage。现在,您将使用SQL参数

插入数据

你需要像SQL语句一样 Insert Into DML_Books_List(ID, [Image]) values (@id, @image)

@id@image是参数的给定名称。您可以通过参数名称设置参数值。

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();
}

答案 2 :(得分:0)

使用参数化查询..

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();