在“文件系统”中插入镜像文件

本文关键字:文件系统 镜像 文件 插入 | 更新日期: 2023-09-27 18:18:27

我得到这个错误:

不允许从数据类型sql_variant隐式转换为唯一标识符。使用CONVERT函数运行此查询。

矿山代码如下:

源代码:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="UserId" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" >
       <InsertItemTemplate>
            <asp:TextBox ID="UserPicUrlTextBox" runat="server" Text='<%# Bind("UserPicUrl") %>' />
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CausesValidation="false" CommandName="Cancel" Text="Clear" />
        </InsertItemTemplate>
</asp:ListView>

矿山插入参数为:

<InsertParameters>
            <asp:Parameter Name="UserId" Type="Object" />
            <asp:Parameter Name="Category" Type="String" />
            <asp:Parameter Name="Class" Type="Int32" />
            <asp:Parameter Name="UserPicUrl" Type="String" />
              .
              .
           </InsertParameters>
c#

:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class userProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        // Get a reference to the currently logged on user
        MembershipUser currentUser = Membership.GetUser();
        // Determine the currently logged on user's UserId value
        Guid currentUserId = (Guid)currentUser.ProviderUserKey;
        // Assign the currently logged on user's UserId to the @UserId parameter
        e.Command.Parameters["@UserId"].Value = currentUserId;
    }
    protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        string virtualFolder = "~/UserPics/";
        string physicalFolder = Server.MapPath(virtualFolder);
        string fileName = Guid.NewGuid().ToString();
        string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
        string UserPicUrl = virtualFolder + fileName + extension;
        e.Command.Parameters["@UserPicUrl"].Value = UserPicUrl;
    }
}

目标:我试图在文件夹中存储图像,并在数据库中保存相应的Url。

在“文件系统”中插入镜像文件

SqlDataSource1_Inserting add set UserId value作为插入参数

e.Command.Parameters["@UserId"].Value =(Guid)Membership.GetUser().ProviderUserKey;