0

我对这个 Web 应用程序的东西有点陌生。当我在 Visual Studios 2012 中为 ASP.NET Web 窗体应用程序创建一个新项目时,它会生成几个预定义的页面/函数。我实际上想使用这些功能,因为它似乎可以节省我一些时间。

在这一点上,我注意到它有一个 Register.aspx 和 Login.aspx,它们工作得很好。问题是我在 Access 2007 中有一个带有一些表的数据库。我想知道是否可以执行以下操作之一以及如何:

1)保留 DefualtConnection 数据库并查询当前登录的用户名,然后使用该用户名查询我的 Access 数据库以获取基于该用户名的信息。

2) 使用 Access 数据库创建我自己的注册和登录。我想知道如何跟踪这种情况下的登录用户,并且在使用创建用户向导时也出现错误

请帮忙,我需要这些信息,以便我可以继续完成我的最终项目。教授不知道如何做到这一点,我一直在网上搜索并回答,但似乎我可能没有问正确的问题。提前致谢 :)

*编辑

•<strong>我的意思是登录用户:图片https://dl.dropbox.com/u/22962879/Project_4_Registro_Est/Logged%20in%20user%20Project4.png

•<strong>默认连接:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Project_4_Registro_Est-20130131171154;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Project_4_Registro_Est-20130131171154.mdf"
  providerName="System.Data.SqlClient" />

•<strong>我的访问数据库

myConn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\cast\Documents\test.accdb")

我的解决方案:

事实证明,我可以通过调用 User.Identity.Name 来获取登录的用户名。所以我做了以下事情:

'//The following code is an example of using the Logged/signed in username to then'
'//Query other Databases based on the user name:'

    Dim myConn As System.Data.OleDb.OleDbConnection

    Dim cmd As New System.Data.OleDb.OleDbCommand


    Dim sqlstring As String

    '//Connecting to My Database:'
    myConn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\cast\Documents\test.accdb")
    '//Query I wish to use to find all data based on User name:'
    sqlstring = "Select FirstName, LastName, UserType FROM users WHERE Username = '" + User.Identity.Name + "'"
    Try
        '//Start by opening the connection'
        myConn.Open()
        '//I use str for now to store the results'
        Dim str As String = ""          

        '//Set the command by adding the SQL string and Connection:'
        cmd = New OleDb.OleDbCommand(sqlstring, myConn)
        '//Create variable which contains results from Executed command:'
        Dim oledbReader As OleDb.OleDbDataReader = cmd.ExecuteReader

        '//Keep reading each row that contains the Queried Results:'
        While oledbReader.Read
            '//Store result to str. each item is a Column in the order I Queried'
            str = str + (oledbReader.Item(0) & " " & oledbReader.Item(1) & " (" & oledbReader.Item(2)).ToString() & ")" + "\n"
        End While

        '//Show results on page's Label1:'
        Label1.Text = str

        '//Close everything'
        oledbReader.Close()
        cmd.Dispose()
        myConn.Close()

    Catch ex As Exception
        '//show error message if could not connect'
        MsgBox("Can not open connection! X_X")
    End Try
4

1 回答 1

0

这应该使用 SimpleMembership。所以问WebMatrix.WebData.WebSecurity.CurrentUserName。也会WebSecurity.IsAuthenticated很好看。

于 2013-02-08T17:14:08.667 回答