【发布时间】:2020-06-15 15:19:41
【问题描述】:
我想用类文件将我的图像保存到数据库中,但是当我尝试上传时,cmd.ExecuteNonQuery(); 出现错误。我对图像列使用 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);
【问题讨论】:
-
你得到什么错误?
-
Image是一个用于操作图像的类,而不是图像的数据。对于数据库,图像只是一个 blob - 一堆字节。在 C# 中,您需要使用这些字节传递byte[]或Stream。 -
@itsme86 此异常最初是在此调用堆栈中引发的:[外部代码] IOOP_Assignment.MyClass.controllers.addclubs(string, string, string, string, string, string, string, string, System. Controllers.cs 中的 DateTime, System.Drawing.Image) IOOP_Assignment.Register.btnRegRegister_Click(object, System.EventArgs) Register.cs [外部代码] Program.cs 中的 IOOP_Assignment.Program.Main()
-
@YogiSeptiargy 但有什么例外?!
-
我怀疑您将 2000-2005 年使用的过时
image类型名称误认为 .NET Image 类型?他们根本没有关系。 SQL Server 有一个varbinary(max)类型,15 多年前称为image。事实上that name is marked for deletion in the future。不要使用这些名称,并更改当前字段以使用正确的名称。无论如何,这只是一个存储字节的 BLOB。
标签: c# sql database forms winforms