C# 使用sql数据库中的其他相关记录更新图像
我有一个名为Phase的表,它包含其他列和一个image列。一切正常,只是当我想更新图像时,它会说“我不能隐式地将nvarchar转换为varbinary(max),而且当我想更新除图像之外的任何一列时,它会显示相同的错误。请帮助我找出我做错了什么?下面分别是我的aspx和aspx.cs代码:C# 使用sql数据库中的其他相关记录更新图像,c#,sql,asp.net,image,gridview,C#,Sql,Asp.net,Image,Gridview,我有一个名为Phase的表,它包含其他列和一个image列。一切正常,只是当我想更新图像时,它会说“我不能隐式地将nvarchar转换为varbinary(max),而且当我想更新除图像之外的任何一列时,它会显示相同的错误。请帮助我找出我做错了什么?下面分别是我的aspx和aspx.cs代码: protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int index = e.RowIndex;
protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
GridViewRow row = (GridViewRow)gv1.Rows[index];
Label eid = (Label)row.FindControl("lbleid");
TextBox date = (TextBox)row.FindControl("txtdate");
DropDownList ddl1 = (DropDownList)row.FindControl("ProjectID");
DropDownList ddl2 = (DropDownList)row.FindControl("ProjectTypeID");
DropDownList ddl3 = (DropDownList)row.FindControl("StateID");
DropDownList ddl4 = (DropDownList)row.FindControl("LGAID");
DropDownList ddl5 = (DropDownList)row.FindControl("CommunityID");
DropDownList ddl6 = (DropDownList)row.FindControl("CoyID");
FileUpload fu = (FileUpload)row.FindControl("fu1");
foreach (HttpPostedFile postedFile in fu.PostedFiles)
{
string filename = Path.GetFileName(postedFile.FileName);
string contentType = postedFile.ContentType;
using (Stream fs = postedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
SqlCommand cmd = new SqlCommand("update Phase set Photo = '" + fu.FileName + "' where Phase_1ID=" + Convert.ToInt32(eid.Text) + "", con);
con.Open();
int res = cmd.ExecuteNonQuery();
con.Close();
}
SqlCommand cmd1 = new SqlCommand("update Phase set DateOfVisit='" + date.Text + "', ProjectTitle='" + ddl1.Text + "', Type='" + ddl2.Text + "', StateName='" + ddl3.Text + "', LGName='" + ddl4.Text + "', CommunityName='" + ddl5.Text + "', CompanyName='" + ddl6.Text + "", con);
con.Open();
int res1 = cmd1.ExecuteNonQuery();
con.Close();
if (res1 == 1)
{
Response.Write("<script>alert('Updation done!')</script>");
}
gv1.EditIndex = -1;
gv1.DataBind();
}
}
}
protectedvoid gv1\u行更新(对象发送方,GridViewUpdateEventArgs e)
{
int index=e.RowIndex;
GridViewRow行=(GridViewRow)gv1.Rows[索引];
Label eid=(Label)row.FindControl(“lbleid”);
TextBox date=(TextBox)row.FindControl(“txtdate”);
DropDownList ddl1=(DropDownList)row.FindControl(“ProjectID”);
DropDownList ddl2=(DropDownList)row.FindControl(“ProjectTypeID”);
DropDownList ddl3=(DropDownList)row.FindControl(“StateID”);
DropDownList ddl4=(DropDownList)row.FindControl(“LGAID”);
DropDownList ddl5=(DropDownList)row.FindControl(“CommunityID”);
DropDownList ddl6=(DropDownList)row.FindControl(“CoyID”);
FileUpload fu=(FileUpload)row.FindControl(“fu1”);
foreach(fu.PostedFiles中的HttpPostedFile postedFile)
{
字符串文件名=Path.GetFileName(postedFile.filename);
字符串contentType=postedFile.contentType;
使用(流fs=postedFile.InputStream)
{
使用(BinaryReader br=新的BinaryReader(fs))
{
byte[]bytes=br.ReadBytes((Int32)fs.Length);
SqlCommand cmd=new SqlCommand(“更新阶段集照片=”“+fu.FileName+”,其中阶段_1ID=“+Convert.ToInt32(eid.Text)+”,con);
con.Open();
int res=cmd.ExecuteNonQuery();
con.Close();
}
SqlCommand cmd1=new SqlCommand(“更新阶段集DateOfVisit=”+date.Text+”,ProjectTitle=“+ddl1.Text+”,Type=“+ddl2.Text+”,StateName=“+ddl3.Text+”,LGName=“+ddl4.Text+”,CommunityName=“+ddl5.Text+”,CompanyName=“+ddl6.Text+”,con);
con.Open();
int res1=cmd1.ExecuteNonQuery();
con.Close();
如果(res1==1)
{
Write(“警报('update done!'));
}
gv1.EditIndex=-1;
gv1.DataBind();
}
}
}
}如果图像列是
Photo
,则您试图存储字符串而不是字节数组(二进制数据)。要存储什么,图像文件名还是图像内容?如果要存储文件名,请将列类型更改为varchar,如果要存储图像数据,请读取图像文件并将其用作内容。此外,还需要一个参数化查询,因为二进制数据不能连接到查询字符串上。@Gusman,tnx。请抽样