0

这是一个简单的 DetailsView,用于在数据库中插入数据。我所有的参数都是以形式设置的。但是,我需要在后面的代码中设置其中一个。此表单用于使用 DetailsVeiw 中的 FileUpload 控件将文件上传到文件系统。但我需要输入上传到数据库的文件路径。我怎样才能做到这一点?在测试代​​码时,我发现表单中的参数正在工作并且数据被插入到数据库中,而不是来自后面代码的那个。提前致谢。

表格:

        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateInsertButton="True" AutoGenerateRows="False" DataKeyNames="attachmentID" DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px" Width="125px"
            OnItemInserted="DetailsView1_ItemInserted">
            <Fields>
                <asp:BoundField DataField="attachmentID" HeaderText="attachmentID" InsertVisible="False" ReadOnly="True" SortExpression="attachmentID" />
                <asp:BoundField DataField="attachmentTitle" HeaderText="attachmentTitle" SortExpression="attachmentTitle" />
                <asp:TemplateField HeaderText="Attach File" SortExpression="FileName">
                    <InsertItemTemplate>
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="attachmentType" HeaderText="attachmentType" SortExpression="attachmentType" />
                <asp:BoundField DataField="attachmentDescription" HeaderText="attachmentDescription" SortExpression="attachmentDescription" />
                <asp:BoundField DataField="attachmentFile" HeaderText="attachmentFile" SortExpression="attachmentFile" InsertVisible="false"/>

            </Fields>
        </asp:DetailsView>
        <asp:Label ID="StatusLabel" runat="server" Text="Label"></asp:Label>
        <br />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SPMS_DBConnectionString1 %>" 
            InsertCommand="INSERT INTO [Attachment] ( [attachmentTitle], [attachmentFile], [attachmentType], [projectID], [attachmentDescription], [attachmentDate]) VALUES ( @attachmentTitle, @attachmentFile, @attachmentType, @projectID, @attachmentDescription, GetDate())" 
            ProviderName="<%$ ConnectionStrings:SPMS_DBConnectionString1.ProviderName %>">

            <InsertParameters>
                <asp:Parameter Name="attachmentTitle" Type="String" />
                <asp:Parameter Name="attachmentType" Type="String" />
                <asp:SessionParameter Name="projectID" SessionField="project" Type="String" />
                <asp:Parameter Name="attachmentDescription" Type="String" />
                <asp:Parameter Name="attachmentFile" Type="String" />
            </InsertParameters>

        </asp:SqlDataSource>

    </div>
</form>

在后面的代码中:这是不影响参数的代码行:

    SqlDataSource1.InsertParameters["attachmentFile"].DefaultValue = Convert.ToString(MapPath("~/AttachedFiles/") + filename);

背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


public partial class stdDocumentsInsert : System.Web.UI.Page
{

string filename;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Directory.Exists(Server.MapPath("~/AttachedFiles/")))
    {
        Directory.CreateDirectory(Server.MapPath("~/AttachedFiles/"));
    }
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{


    FileUpload fu1 = (FileUpload)this.DetailsView1.FindControl("FileUpload1");
    //if (fu1 == null)
    //{
    //    //e.Cancel = true;
    //    StatusLabel.Text = "Could not find file upload";
    //}
    if (fu1.HasFile)
    {
        try
        {
            filename = Path.GetFileName(fu1.PostedFile.FileName);
            fu1.SaveAs(Server.MapPath("~/AttachedFiles/") + filename);
            StatusLabel.Text = "Upload status: File uploaded!";
            SqlDataSource1.InsertParameters["attachmentFile"].DefaultValue = Convert.ToString(MapPath("~/AttachedFiles/") + filename);
            //SqlDataSource1.insert(); // when i used this it worked but other fields in DB became Null


        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }

    }
    else
    {
        //e.Cancel = true;
        StatusLabel.Text = "No file uploaded";
        return;
    }
}
}
4

2 回答 2

0

来自MSDN DetailsView.ItemInserted 事件:

“在单击 DetailsView 控件中的插入按钮时发生,但在插入操作之后。”

所以你在插入后设置值。您的代码对于设置插入参数是正确的,您只需在 DetailsView.ItemInserting 事件中调用它

于 2013-07-14T06:25:04.770 回答
-2

在数据库本身中添加一个名为 tc 的 varbinary 字段,然后执行以下操作:

Protected Sub SqlDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting
        Dim FU As New FileUpload
        FU = DetailsView1.FindControl("fileupload1")    
        Using fs As Stream = FU.PostedFile.InputStream
            Using br As New BinaryReader(fs)
                Dim tc As SqlParameter
                tc = New SqlParameter("@tc", SqlDbType.VarBinary)
                Dim bytes As Byte() = br.ReadBytes(DirectCast(fs.Length, Long))
                tc.Value = bytes
                e.Command.Parameters.Add(tc)

            End Using
        End Using       
    End Sub
于 2015-08-09T14:53:43.680 回答