【问题标题】:C# asp.net logging clients' activitiesC# asp.net 记录客户端的活动
【发布时间】:2015-04-17 09:58:43
【问题描述】:

数据库表:注册和购买 (使用 1-10000 范围内的唯一 ID 作为主键和外键连接两个表)

当有相应ID的游客进行在线购买时,如何在我的购买表中添加唯一ID?

我正在使用 Visual Studio 开发 asp.net。但是,如何存储客户端的唯一 ID 以识别他们的日志记录活动?

        string result = "0";
        OleDbConnection connection = new OleDbConnection();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sim\Desktop\Web.accdb";
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "select ID from Registration where Name ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "'";

        string getValue = command.ExecuteScalar().ToString();
        if (getValue != null)
        {
            result = getValue.ToString();
        }
        Session.Add("UserID", result);

        command.CommandText = "select * from Registration where Name ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "'";

        OleDbDataReader reader = command.ExecuteReader();
        int count = 0;

        while (reader.Read())
        {
            count = count + 1;
        }
        if (String.IsNullOrEmpty(TextBox1.Text))
        {
            MessageBox.Show("You have't input the Username.");
        }
        if (String.IsNullOrEmpty(TextBox2.Text))
        {
            MessageBox.Show("you havn't input the Password.");
        }
        if (count == 1)
        {   

            MessageBox.Show("Username and password is valid");
            connection.Close();
            connection.Dispose();
            Response.Redirect("Loginpage.aspx", true);
        }
        else
        {
            MessageBox.Show("Username or Password is not matched");
        }
    }

【问题讨论】:

  • 客户活动是什么意思?
  • 我想保存客户对应的唯一ID,以便在客户进行网上购物活动时,我可以穿上唯一ID
  • 使用 Process.GetCurrentProcess().SessionId 获取会话 ID,这对于每个用户的每个会话都是唯一的。
  • 在登录页面使用???然后我如何将相应的唯一 ID 标记到采购表中>
  • 是的,这将一直存在到用户结束其会话。因此,如果用户有一个唯一 ID,并且他将购买任何产品,您可以将该 ID 与购买表绑定

标签: asp.net sql-server c#-4.0 relational-database primary-key


【解决方案1】:

您可以尝试这样的方法来获取您的注册(客户)ID:

public static string GetClientId()
{
    string result = "0";
    OleDbConnection connection = new OleDbConnection();
    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sim\Desktop\Web.accdb";
    connection.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "select ID from Registration where Name ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "'";

    string getValue = command.ExecuteScalar().ToString();
    if (getValue != null)
    {
        result = getValue.ToString();
    }
    conn.Close();
    return result;
}

您可以使用以下方法将该 ID 保存在 Session 中:

Page.Session["UserID"] = GetClientId();

然后,一旦购买,您可以通过检索该值来保存它:

string id = Page.Session["UserID"];

最后,购买时,您需要将其插入数据库中。要了解如何做到这一点,你可以看看这个:C# Inserting Data from a form into an access Database

【讨论】:

  • 是的,这就是我的想法。但是,我想知道如何为登录的用户建立一个会话,以将他们的 ID 标记到购买表中。我没有这个想法...
  • 好的,我在回答中添加了更多详细信息
  • i am so new... 请问Getvalue的存在是什么意思?和 Page.Session["UserID"] = GetClientId();
  • getValue 只是一个保存查询结果的变量。 ExecuteScalar 是此代码中的重要关键字,因为它允许获取单个值而不是行/表。关于Page.Session,我建议阅读:msdn.microsoft.com/en-us/library/ms178581%28v=vs.140%29.aspx
  • 你能帮我看看上面的代码吗?我怎样才能使用两个 command.commandtext ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 2014-09-12
  • 2012-06-30
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多