【问题标题】:upload employee image into SQL database along with other info with insert query使用插入查询将员工图像与其他信息一起上传到 SQL 数据库
【发布时间】:2015-04-15 19:43:48
【问题描述】:

我在使用 c# 将我的员工照片上传到数据库时遇到问题。我有一个员工输入屏幕表格。在该表格上有关于员工的各种信息,例如(姓名、地址、薪水、入职日期、出生日期、图像、手机号码等)。

我的插入查询在没有图像的情况下运行良好。这是我的代码示例。

 query = "insert into tbl_emp values('" + txtempid.Text + "','" + txtempname.Text + "','"+ cmbSex.Text +"','"
                + cmbDepartment.Text +"','" + cmbEmpDesig.Text + "','" + cmbemptyp.Text + "','" 
                + txtsalary.Text + "','"+ txtParmanentAdd.Text +"','"+ txtPresentAdd.Text +"','"
                + txtContactNo.Text +"','"+ txtEmailAdd.Text +"','"+ dateBirth.Text +"','" 
                + dateJoin.Text + "','"+ txtCardNo.Text +"')";
           con.executeCmd_Sql(query);

请建议我如何插入图像以及这些其他信息。在此先感谢:)

【问题讨论】:

标签: c# image insert


【解决方案1】:

(假设您使用的是 ASP.NET 网络表单) .aspx:

 <asp:Image runat="server" ID="img" ImageUrl="~/Chrysanthemum.jpg" />

代码隐藏 (.cs):

 string pic = img.ImageUrl;

将实际图片(如二进制数据)保存到数据库中并不是一个最佳实践。这就是为什么您通常只获取图片的路径(如"~/mypicture.jpg")并将其作为字符串保存在数据库中。所以基本上,您所要做的就是在查询中传递字符串pic

无关:但您的查询对 SQL 注入非常开放。我建议您阅读此link

【讨论】:

    【解决方案2】:

    插入图像很容易。您只需要将图像转换为字节数组。看看这个代码片段示例:

    private void updatedata()
    {
    
        //use filestream object to read the image.
    
        //read to the full length of image to a byte array.
    
        //add this byte as an oracle parameter and insert it into database.
    
        try
        {
    
            //proceed only when the image has a valid path
    
            if (imagename != "")
            {
    
                FileStream fs;
    
                fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
    
                //a byte array to read the image
    
                byte[] picbyte = new byte[fs.Length];
    
                fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
    
                fs.Close();
    
                //open the database using odp.net and insert the data
    
                string connstr = @"Data Source=.;Initial Catalog=TestImage;
                Persist Security Info=True;User ID=sa";
    
                SqlConnection conn = new SqlConnection(connstr);
    
                conn.Open();
    
                string query;
    
                query = "insert into test_table(id_image,pic) values(" + 
                textBox1.Text + "," + " @pic)";
    
                SqlParameter picparameter = new SqlParameter();
    
                picparameter.SqlDbType = SqlDbType.Image;
    
                picparameter.ParameterName = "pic";
    
                picparameter.Value = picbyte;
    
                SqlCommand cmd = new SqlCommand(query, conn);
    
                cmd.Parameters.Add(picparameter);
    
                cmd.ExecuteNonQuery();
    
                MessageBox.Show("Image Added");
    
                cmd.Dispose();
    
                conn.Close();
    
                conn.Dispose();
    
                Connection();
    
            }
    
        }
    
        catch (Exception ex)
        {
    
            MessageBox.Show(ex.Message);
    
        }
    
    }
    

    如果您需要更多信息,请查看this website,它解释了您需要了解的所有信息。我从那个网站粘贴到这里的这段代码片段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多