现有在sql server中有表student,字段为studentid(int),Name(varchar), photo(image),请问如何在表中保存和提取图片等。老师要求保存和提取该数据表中的信息(包括图片)全部用存储过程来实现,在WINFORM下开发,C/S形式,高手帮们给段代码吧!小弟初学C#,给段详细代码过程小弟拜谢了!!!!

解决方案 »

  1.   

    上传图片 If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then
    Exit Sub
    End If
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    With PictureBox1
    .Image = Image.FromFile(OpenFileDialog1.FileName)
    .SizeMode = PictureBoxSizeMode.CenterImage
    .BorderStyle = BorderStyle.Fixed3D
    End With
    End IfDim ms As New MemoryStream()
    PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
    Dim arrImage() As Byte = ms.GetBuffer
    ms.Close()
    Dim imgid As String = CInt(Int(10 * Rnd()) + Int(100 * Rnd()) + Int(1000 * Rnd()) + Int(10000 * Rnd()) + Int(100000 * Rnd()) + Int(1000000 * Rnd()) + Int(10000000 * Rnd())).ToString
    Try
    Dim conn As New GWConnection()
    Dim northwindConnection As New SqlConnection()
    northwindConnection = conn.GetCN()Dim strSQL As String = "INSERT INTO t2_005(imgid,pactcode,Picture) VALUES(@imgid,@pactcode,@Picture)"
    Dim cmd As New SqlCommand(strSQL, northwindConnection)
    With cmd
    .Parameters.Add(New SqlParameter("@imgid", SqlDbType.NVarChar, 50)).Value = imgid
    .Parameters.Add(New SqlParameter("@pactcode", SqlDbType.NVarChar, 16)).Value = txt_pactcode.Text
    .Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrImage
    End With
    If Not northwindConnection.State = ConnectionState.Open Then
    northwindConnection.Open()
    End If
    cmd.ExecuteNonQuery()
    northwindConnection.Close()
    'txt_imgid.Text = imgid
    Catch ex As SqlException
    MsgBox("图片上传失败!", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "出错提示!")
    End Try
     
      

  2.   

    // Source Code for Save the image file into the databasepublic void OnUpload(Object sender, EventArgs e)
    {
        // Create a byte[] from the input file
        int len = Upload.PostedFile.ContentLength;
        byte[] pic = new byte[len];
        Upload.PostedFile.InputStream.Read (pic, 0, len);
        // Insert the image and comment into the database
        SqlConnection connection = new 
          SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
        try
        {
            connection.Open ();
            SqlCommand cmd = new SqlCommand ("insert into Image " 
              + "(Picture, Comment) values (@pic, @text)", connection);
            cmd.Parameters.Add ("@pic", pic);
            cmd.Parameters.Add ("@text", Comment.Text);
            cmd.ExecuteNonQuery ();
        }
        finally 
        {
            connection.Close ();
        }
    }private void GetImage(string str)
    {
    int num=this.dataGrid1.CurrentRowIndex;
    if(num==-1)
    {
    return;
    }
    if(dataset.Tables[0].Rows[num]["照片"]!=Convert.DBNull)
    {
    byte[] bytes=(byte[])dataset.Tables[0].Rows[num]["照片"];
    MemoryStream memStream=new MemoryStream(bytes);
    Bitmap bitmap = new Bitmap(memStream);
    memStream.Close();
    this.pictureBox1.Image = bitmap;
    }
    else
    {
    this.pictureBox1.Image = null;
    }
    }
      

  3.   

    添加图片:http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506显示图片:http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1我用的就是这个,好用的。