我需要在更新查询中使用下拉列表中的值

时间:2013-10-25 01:55:50

标签: c# asp.net sql

所以我有一个fileupload控件,用于将图片上传到我的服务器。没有下拉列表我没有问题,但我需要从下拉列表中的值来指定我想在数据库中更新哪条记录。这是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;
        }
    }
}

每当我尝试上传文件时,它都会告诉我update语句的语法有错误。所以我真的不知道如何使用DDL中的值来指定数据库中我想要的记录。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

对于TSQL更新查询有点像这样

UPDATE TABLENAME SET COLUMN1='valuesyouwant' where='whatever'

对于您的查询,它应该是这样的

"UPDATE Table2 SET Image=@Image WHERE ID=@ID"