简体   繁体   English

在C#ASP.NET中更新数据库表单元

[英]Update a database table cell in c# asp.net

I have a login page linked to a login db table with 3 columns namely ID , Pass and AccessTime . 我有一个登录页面链接到具有3列的登录数据库表,即IDPassAccessTime Whenever the login ID is successful, I need to update the respective record with the date and time of access. 只要登录ID成功,我就需要用访问的日期和时间更新相应的记录。

UPDATE Login SET AccessTime = '"+DateTime.Now+"' WHERE ID = '" + id + "' ;"

And, retrieve the ID and the AccessTime in another page in a textbox. 并且,在文本框中的另一个页面中检索IDAccessTime

I've used sessions for the retrieval process. 我在检索过程中使用了会话。

Now, how do I implement this on C# ASP.NET? 现在,如何在C#ASP.NET上实现此功能? I'm a newbie... 我是新手

First , learn the basics of ASP.NET http://www.asp.net/web-pages/overview/getting-started 首先 ,学习ASP.NET的基础知识http://www.asp.net/web-pages/overview/getting-started

Second , Create a procedure ,创建程序

      ALTER PROCEDURE SP_TRACK_USERLOG
      @id varchar(100),
      @password varchar(100)
      AS
      BEGIN
          SET NOCOUNT ON;
          INSERT INTO LOGIN(ID,Pass ,AccessTime)  VALUES (@id,@password ,GETDATE())

       END

Third , In SERVER side call the SP as follow ,在SERVER端调用SP如下

     SqlCommand cmd = new SqlCommand("SP_TRACK_USERLOG", con);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.AddWithValue("@id", Session["id"].ToString());
     cmd.Parameters.AddWithValue("@password", Session["password"].ToString());
     cmd.ExecuteNonQuery();

Also, learn about TimeZone Daylight saving time and time zone best practices . 另外,了解TimeZone 日光节约时间和时区最佳实践 This will help in long run. 从长远来看,这将有所帮助。

Learn about Connection String in asp.net How to create a connection string in asp.net c# 了解连接字符串在asp.net 如何建立在asp.net C#中的连接字符串

You should use using : 您应该使用使用

using System.Data;
using System.Data.SqlClient;

Then create your connection, open your connection, write your SQL command, and SQL adapter 然后创建您的连接,打开您的连接,编写您的SQL命令和SQL适配器

    static void Main(string[] args)
    {
        SqlConnection conn = new SqlConnection(@"Data Source = .\SQLEXPRESS; Initial Catalog=AdventureWorks2014; Integrated Security=SSPI");
        conn.Open();

In the sql command you should use sql parameters 在sql命令中,您应该使用sql参数

        //somthing like thes
        SqlParameter param = new SqlParameter();
        param.ParameterName = "@param";
        param.Value = TheValue;

Then you could use them in the sql command 然后可以在sql命令中使用它们

        SqlCommand cmd = new SqlCommand("UPDATE Login SET AccessTime = '"+DateTime.Now+"' WHERE ID = '" + id + "' ;"", conn);
      //add parameters
       cmd.Parameters.Add(param);
       cmd.ExecuteScalar();
       conn.Close();
       conn.Dispose();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM