【问题标题】:how to store list of files in mysql database and display them based on users role(s)如何在mysql数据库中存储文件列表并根据用户角色显示它们
【发布时间】:2012-04-25 07:32:28
【问题描述】:

我是一个 asp.net mvc 新手。自从经典的asp以来,我还没有真正做过任何事情。我想根据用户角色显示来自 Mysql 数据库的文件列表。到目前为止,我已经创建了两个表,其中一些文件可以由多个角色查看。我也在使用 MySql Membership Provider

表格文件

FileID, FileName, FilePath

文件角色

FileID, RoleID

我想我可以将一个文件添加到 files 表中,然后对于有权访问该文件的每个角色,我需要将 fileID 添加到 FilesRole 表中,并从 my_aspnet_roles 表中获取 RoleID并将其添加到。

然后当有人去查看文件时,它会以某种方式获取他们所在的每个角色的 ID,然后从数据库中获取文件列表

我不知道从哪里开始,所以任何帮助都将不胜感激。

【问题讨论】:

  • 对我来说一切都很好,继续前进:)
  • 你可以从很多地方开始。在codefirst中,您通过创建表格搞砸了:(谷歌教程,问题太宽泛了。

标签: asp.net mysql asp.net-mvc-3 roles


【解决方案1】:

这是我目前所拥有的。它可以工作,但我知道这是不正确的,因为我在 Controller 中做数据库的东西,应该在模型中完成,但我不知道如何以这种方式完成。

型号

public class files
{
    public int fileid { get; set; }
    public string fileName { get; set; }
    public string filePath { get; set; }
}

控制器

public ActionResult Index()
    {
        IList<files> downloads = new List<files>();
        // Get Users Role(s) thanks Ropstah
        string[] userRoles = Roles.GetRolesForUser(User.Identity.Name);
        string userRolesForSqlQuery = userRoles.Select(x => "'" + x + "'").Aggregate((a, b) => a + "," + b);

        // MySql Connection thanks again Ropstah
        string connectionString = "my data source";
        var MySqlConnectionString = String.Format(@"SELECT fileid, fileName, filePath, FROM downloads
                                                    INNER JOIN downloadsroles on downloads.fileid = downloadsroles.downloadsid
                                                    WHERE downloadsroles.roleName IN ({0});", userRolesForSqlQuery);

        MySqlConnection conn = new MySqlConnection(connectionString);
        MySqlCommand comm = conn.CreateCommand();
        MySqlDataReader Reader;
        comm.CommandText = MySqlConnectionString;
        conn.Open();
        Reader = comm.ExecuteReader();

        while (Reader.Read())
        {
            downloads.Add(new files() { fileName = Reader["fileName"].ToString(),
                                        filePath = Reader["filePath"].ToString()});
        }

        conn.Close();

        return View(downloads);
    }

查看

@foreach (var downloads in ViewData.Model)
{
<tr>
    <td>
        @downloads.fileName
    </td>
    <td>
        @downloads.filePath
    </td>
</tr>
}

【讨论】:

    【解决方案2】:

    让我让你快速了解:

    1。创建用户并将其添加到角色

    Webdeveloper Express 和 Visual Studio 为您的网站提供了一个 Web 管理工具:

    要访问网站管理工具,请在网站菜单上单击 ASP.Net 配置。

    参考:http://msdn.microsoft.com/en-us/library/yy40ytx0.aspx

    您可以从那里添加用户和角色以及添加用户到角色。

    2。创建数据库记录(假设您有某种 mysql 管理面板)

    现在在您的数据库中创建一些文件记录,并在文件和角色之间创建一些关联记录。我会使用 RoleName 而不是 RoleID,因为默认 RoleProvider 接口适用于 string 名称。

    3。了解如何从 ASP.NET 查询 mysql 数据库

    例如阅读:http://support.discountasp.net/KB/a358/how-to-query-a-mysql-database-in-aspnet.aspx

    4。根据用户角色创建正确的查询

    string[] userRoles = Roles.GetRolesForUser(HttpContext.Current.User.Identity.Name);
    
    // creates "'role1','role2' from array.
    string userRolesForSqlQuery = userRoles.Select(x => "'" + x + "'").Aggregate((a,b) => a + "," + b); 
    
    
    //Psuedo code for querying the database...
    var fileForUserQuery = String.Format("SELECT FileID, FileName, FilePath FROM Files 
                               INNER JOIN FilesRole ON Files.FileID = FilesRole.FileID
                               WHERE FilesRole.RoleName IN ({0});", userRolesForSqlQuery);
    

    希望这能让你到达那里......

    【讨论】:

      猜你喜欢
      • 2021-10-03
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多