从vb.net在sql数据库中插入2个图像

时间:2015-07-16 11:00:21

标签: sql-server vb.net

我的sql 2005数据库中有一个表租户。它具有以下成员,其中tdoc和tpic的数据类型为image

  • TID
  • TNAME
  • TADD
  • TPH
  • tdoc
  • TPIC

我想使用VB.NET将2个图片框中的2张图片插入tdoctpic

我设法为我的记录保存按钮编写以下代码。请帮我提供更多代码。

Private Sub bsave_Click(sender As Object, e As EventArgs) Handles bsave.Click
    If ((tname.Text = "") Or (tadd.Text = "") Or (tphn.Text = "")) Then
        MsgBox("Details are Incomplete", MsgBoxStyle.Exclamation)
    Else
        conn.Open()
        Dim s As String = "Insert into rent values('" & tid & "','" & tname.Text & "','" & tadd.Text & "','" & tphn.Text & "',@doc,@photo)"


        MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK)
        conn.Close()
    End If
End Sub

我想使用MemoryStream,请指导我。

1 个答案:

答案 0 :(得分:0)

我在C#项目中使用了这个逻辑,希望它可以帮到你。

using System.IO;

private void **BrowseFile_Click**(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();

    dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.pnbg)|*.png|All Files(*.*)|*.*";
    dlg.Title = " Select Bike Picture";

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        String picLoc = dlg.FileName.ToString();
        pictureBox1.ImageLocation = picLoc;
        textBox9.Text = picLoc;
    }
}

private void **SaveButton_Click**(object sender, EventArgs e)
{
    FileStream fs1 =newFileStream(textBox9.Text,System.IO.FileMode.Open, System.IO.FileAccess.Read);

    byte[] image = new byte[fs1.Length];

    fs1.Read(image, 0, Convert.ToInt32(fs1.Length));
    fs1.Close();

    con.Open();

    SqlCommand cmdupdt = new SqlCommand("Insert into TableName values(@pic )",con);
    SqlParameter prm = new SqlParameter("@pic", SqlDbType.VarBinary, image.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, image);

    cmdupdt.Parameters.Add(prm);
    cmdupdt.ExecuteNonQuery();

    con.Close();
}