我需要在更新查询中使用下拉列表中的值
本文关键字:下拉列表 查询 更新 | 更新日期: 2023-09-27 18:36:45
所以我有一个文件上传控件,用于将图片上传到我的服务器。我已经让它在没有下拉列表的情况下正常工作,但我需要下拉列表中的值来指定我要在数据库中更新的记录。下面是 aspx:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/TravelJoansDB.mdb"
SelectCommand="SELECT * FROM [Table2] ORDER BY ID DESC"
UpdateCommand="UPDATE Table2 SET (Image) VALUES (@Image) WHERE ID=@ID" >
<UpdateParameters>
<asp:Parameter Name="Image" Type="String" />
<asp:Parameter Name="ID" Type="Int16" />
</UpdateParameters>
</asp:AccessDataSource>
<asp:Label ID="SelectBlogLabel" runat="server" Text="Select a Blog ID for the photo that you are uploading" /><br />
<asp:DropDownList ID="BlogPostTitleDDL" runat="server"
AutoPostBack="true"
DataSourceID="AccessDataSource1"
DataValueField="ID" /><br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="UploadButton" runat="server" OnClick="UploadFile" Text="Upload photo" /><br />
<asp:Label ID="UploadStatusLabel" runat="server" Text="Status: " /><br />
下面是背后的代码:
protected void UploadFile(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
if (FileUpload1.PostedFile.ContentType == "image/jpeg")
{
if (FileUpload1.PostedFile.ContentLength < 10240000)
{
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/PlaceImages/") + filename);
UploadStatusLabel.Text = "Upload status: Complete!";
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|'TravelJoansDB.mdb;";
string cmdstr = "UPDATE Table2 (Image) VALUES (@Image) WHERE ID=@ID";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
int blogID = BlogPostTitleDDL.SelectedIndex;
con.Open();
com.Parameters.AddWithValue("@Image", filename);
com.Parameters.AddWithValue("@ID", blogID);
com.ExecuteNonQuery();
con.Close();
}
else
UploadStatusLabel.Text = "Upload status: The file has to be less than 10 MB!";
}
else
UploadStatusLabel.Text = "Upload status: Only JPEG files are accepted!";
}
catch (Exception ex)
{
UploadStatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
每当我尝试上传文件时,它都会告诉我更新语句的语法有错误。所以我只是不知道如何使用 DDL 中的值来指定我想要的数据库中的哪条记录。任何帮助将不胜感激。
对于 TSQL 更新查询是这样的
UPDATE TABLENAME SET COLUMN1='valuesyouwant' where='whatever'
对于您的查询,它应该是这样的
"UPDATE Table2 SET Image=@Image WHERE ID=@ID"