将图像(字节)插入到具有3层的DB

时间:2015-04-23 16:02:44

标签: c# image bytearray converter picturebox

我希望在数据库中插入pic,其中包含UI层中的过程和3层对接错误! 我在我的项目中有一个程序,数据访问层和业务逻辑层...... 在Btn_Insert代码中,输入值发送到params ...然后发送到SP!

此商店程序:

    ALTER PROCEDURE [dbo].[Agent_Insert]
    @Agent NVARCHAR(MAX), 
    @Mob NVARCHAR(MAX),
    @Pic IMAGE, 
    @Toz NVARCHAR(MAX)
    AS 
    BEGIN 
    INSERT INTO Agent(Agent, Mob, Pic, Toz) 
    VALUES (@Agent, @Mob, @Pic, @Toz) 
    END

此DAL代码:

public int Agent_Insert(string Agent, string Mob, byte Pic, string Toz)
    {
        var returnValue = 0;
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand dCmd = new SqlCommand("Agent_Insert", conn))
            {
                dCmd.CommandType = CommandType.StoredProcedure;
                SqlParameter[] prms = new SqlParameter[4];
                prms[0] = new SqlParameter("@Agent", SqlDbType.NVarChar);
                prms[0].Value = Agent;
                prms[1] = new SqlParameter("@Mob", SqlDbType.NVarChar);
                prms[1].Value = Mob;
                prms[2] = new SqlParameter("@Pic", SqlDbType.Image);
                prms[2].Value = Pic;
                prms[3] = new SqlParameter("@Toz", SqlDbType.NVarChar);
                prms[3].Value = Toz;
                dCmd.Parameters.AddRange(prms);
                conn.Open();
                returnValue = dCmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        return returnValue;
    }

此BLL代码:

public class Agent_Bll
{
    public int Insert(string Agent, string Mob, byte Pic, string Toz)
    {
        return new Agent_Dal().Agent_Insert(Agent, Mob, Pic, Toz);
    }

    public int Update(int Id, string Agent, string Mob, byte Pic, string Toz)
    {
        return new Agent_Dal().Agent_Update(Id, Agent, Mob, Pic, Toz);
    }

    public int Delete(int Id)
    {
        return new Agent_Dal().Agent_Delete(Id);
    }

    public DataTable Select()
    {
        return new Agent_Dal().Agent_Select();
    }
}

这是我的代码形式:

public static byte[] ImageToByte(Image img)  
    { 
        ImageConverter converter = new ImageConverter(); 
        return (byte[])converter.ConvertTo(img, typeof(byte[])); 
    } 


    private void Btn_Insert_Click(object sender, EventArgs e) 
    { 
       var result = 0; 
       try 
       { 
           result = new Agent_Bll().Insert(Txt_Agent.Text.Trim(), Txt_Mob.Text.Trim(), pictureBox1.Image(ImageToByte), Txt_Toz.Text.Trim());  

对接错误:(无字节)

pictureBox1.Image(ImageToByte)

1 个答案:

答案 0 :(得分:0)

在DAL和BLL方法中放近字节... []!

byte[]