【问题标题】:How to save a picture to an access database table?如何将图片保存到access数据库表中?
【发布时间】:2014-03-03 20:58:39
【问题描述】:

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

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

但它有一些问题。

请帮我解决这个问题。

谢谢

【问题讨论】:

  • 您能告诉我们什么问题吗?请编辑问题并发布您的错误消息及其出现的行。

标签: c# database image ado.net send


【解决方案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

【讨论】:

    【解决方案2】:

    使用如下代码的参数:

    假设您的表中有两列名为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();
    }
    

    【讨论】:

    • 你好。你能描述一下这段代码吗?你能告诉我 ID、@p1、@p2、textbox1 和 pic 是什么吗?谢谢
    • 我正在使用此代码code var pic = File.ReadAllBytes(Picture_Path); using (OleDbCommand cmd = new OleDbCommand("Insert Into PIC(Image) values ('"+Picture_Path+"')", con)) { con.Open(); cmd.Parameters.AddWithValue(Picture_Path, pic); cmd.ExecuteNonQuery(); } code 但它不起作用。
    【解决方案3】:

    使用参数化查询..

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      相关资源
      最近更新 更多