c#使用类将图像插入数据库

wd2eg0qa  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(218)

我想用类文件将图像保存到数据库,但在中出现错误 cmd.ExecuteNonQuery(); 当我尝试上传的时候。我对image列使用bin数据类型。这是我的班级代码

public int addclubs(string clubname, string president, string presidentID, string vicepresident, string vicepresidentID, string secretary, string secretaryID,
                          string clubdesc, DateTime established, Image images)
    {
        int status = 0;

        string insertSQL = "INSERT INTO Clubs(club_name,president,presidentID,vice_president,vice_presidentID,secretary,secretaryID,club_desc,established,image)" +
            "Values(@club,@prs,@prsID,@vc,@vcID,@sec,@secID,@clubdesc,@esb,@img)";
        Connect();
        SqlCommand cmd = new SqlCommand(insertSQL, conn);
        cmd.Parameters.AddWithValue("@club", clubname);
        cmd.Parameters.AddWithValue("@prs", president);
        cmd.Parameters.AddWithValue("@prsID", presidentID);
        cmd.Parameters.AddWithValue("@vc", vicepresident);
        cmd.Parameters.AddWithValue("@vcID", vicepresidentID);
        cmd.Parameters.AddWithValue("@sec", secretary);
        cmd.Parameters.AddWithValue("@secID", secretaryID);
        cmd.Parameters.AddWithValue("@clubdesc", clubdesc);
        cmd.Parameters.AddWithValue("@esb", established);
        cmd.Parameters.AddWithValue("@img", images);
        cmd.ExecuteNonQuery();

        return status;
    }

来这里是为了 private void btnRegRegister_Click(object sender, EventArgs e) ```
byte[] imagebt = null;
FileStream fst = new FileStream(picUploadReg.ImageLocation, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fst);
imagebt = br.ReadBytes((int)fst.Length);
DateTime established = DateTime.Parse(dtpClubReg.Value.ToString());
ctrls.addclubs(txtNameClubReg.Text, txtPresidentReg.Text, txtPresidentIDReg.Text, txtViceReg.Text, txtViceIDReg.Text, txtSecReg.Text, txtSecIDReg.Text, txtClubDescReg.Text, established,picUploadReg.Image);

bttbmeg0

bttbmeg01#

我建议不要将图像本身保存在表中。您可能希望在应用程序内的文件夹中保存或创建图像的副本,以及对表中图像的引用。从我的经验来看,这是更容易和更有效的。我很乐意给你提供代码,一旦我回家,因为我在移动。

相关问题