如何使用 c# 在访问数据库中添加时间戳 [英] how to do time stamping in access db using c#

查看:77
本文介绍了如何使用 c# 在访问数据库中添加时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
{
    OleDbConnection mycon = new OleDbConnection();
mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1.accdb";
    OleDbCommand command = new OleDbCommand();
    command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
    mycon.Open();
    command.Connection = mycon;
    command.ExecuteNonQuery();
    mycon.Close();
}

这是我编写的用于在访问数据库中插入一些详细信息的代码.我希望此按钮单击还将单击的日期和时间添加到我的数据库中的列中.我试图在 INSERT 中直接添加一个函数 GetDate 但它无法执行.有什么建议吗?

this is the code that I have written to insert some details in my access db. I want that this button click also add the date and time of click into a column in my db. I tried to directly add a function GetDate within INSERT but it failed to execute. Any suggestions?

推荐答案

我还没有测试过这段代码,但它应该能让你开始......

I haven't tested this code, but it should get you started...

private void button1_Click(object sender, EventArgs e)
{
    using OleDbConnection mycon = new OleDbConnection()
    {
        mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1.accdb";

        OleDbCommand command = new OleDbCommand();
        command.CommandText = "INSERT INTO Table1 (Emp_ID, Asset_ID, Date_Column) VALUES (?, ?, ?)";

        command.Parameters.Add("@EmpID", OleDbType.VarChar, 80).Value = textBox1.Text;
        command.Parameters.Add("@AssetID", OleDbType.VarChar, 80).Value = textBox2.Text;
        command.Parameters.Add("@Timestamp", OleDbType.Date).Value = DateTime.Now;

        command.Connection = mycon;
        mycon.Open();
        command.ExecuteNonQuery();
    }
}

这篇关于如何使用 c# 在访问数据库中添加时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆