如何立即插入日期时间并选择当天已插入的数据 [英] how to insert datetime now and select the data that has been inserted on that day

查看:67
本文介绍了如何立即插入日期时间并选择当天已插入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

您能帮我吗:我想将数据插入sql数据库,然后将最新数据绑定到gridview.latest表示已在1进程中插入的数据.不是表的最后一行.

所以我想出了一个插入datetime.now到表sql数据库中的想法,然后我想显示已插入的数据.但是我仍然没有解决方案.请有人帮帮我...

hi guys,

can you help me on this: i want to insert data into sql database and then i want to bind that latest data into gridview.latest means data that has been inserted in 1 process.not the last row in table.

so i come out with the idea insert a datetime.now into table sql database.and then i want to display the data that has been inserted.but i still got no solution.pls someone help me...

protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection cn0 = new SqlConnection(connectionString);
        cn0.Open(); 
        string l = "insert into approval (name_item, quantity, stock_last,name,staff_no,date) values (@name_item, @quantity, @stock_last, @name, @staff_no, @date)";
        SqlCommand c = new SqlCommand(l, cn0);

        //TextBox8.Text = dt.ToString();

        c.Parameters.Add("@name_item", SqlDbType.VarChar).Value = ddl1.SelectedItem.Text.Trim();
        c.Parameters.Add("@quantity", SqlDbType.Int).Value = TextBox7.Text.Trim();
        c.Parameters.Add("@stock_last", SqlDbType.Int).Value = kuantiti;
        c.Parameters.Add("@name", SqlDbType.VarChar,50).Value = TextBox3.Text.Trim();
        c.Parameters.Add("@staff_no", SqlDbType.Int).Value = TextBox2.Text.Trim();
        c.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Now;// = TextBox8.Text;// = TextBox8.Text;

        cd.ExecuteNonQuery();
        c.ExecuteNonQuery();
    }

protected void Button5_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        conn.Open();

        DateTime dt = DateTime.Now;
        SqlConnection con0 = new SqlConnection(connectionString);
        string s = "select convert(varchar(11), date, 103)as date from approval where staff_no = '" + TextBox2.Text + "'";
        SqlCommand cm = new SqlCommand(s,conn);
        //cm.CommandText = 
        //cm.Connection = con0;
        //con0.Open();
        string name = ((string)cm.ExecuteScalar());
        TextBox8.Text = ((string)cm.ExecuteScalar());
        SqlDataAdapter sql = new SqlDataAdapter(s, conn);
        DataSet ds = new DataSet();

        sql.Fill(ds, "approval");
        GridView1.DataSource = ds.Tables["approval"].DefaultView;
        GridView1.DataBind();
    }


预先感谢
musiw.


thanx in advance
musiw.

推荐答案

在表中新建一个名为DateTime 数据类型的CreatedDate 的字段,并将其默认值设置为getdate().
Make a new field in your table named like CreatedDate of DateTime datatype and set its default value to getdate().



考虑在表中添加一个名为"inserted_Order"的新列.然后在每个插入过程中增加该列的值.然后,您将能够检索在每个过程中插入的记录集.

问候
Sebastian
Hi,
Think about adding a new column called "inserted_Order" in your table. Then increment that column value in each insert process. Then you will be able to retrieve record set you have inserted in each process.

Regards
Sebastian


如果您数据库中日期列的数据类型为datetime,那么您还可以使用getdate SQL函数插入日期.

试试:
插入批准(name_item,数量,stock_last,name,staff_no,date)值(@ name_item,@ quantity,@ stock_last,@ name,@ staff_no,getdate())
if your data type of date column in database is datetime then u can also use the getdate SQL function to insert date.

Try:
insert into approval (name_item, quantity, stock_last,name,staff_no,date) values (@name_item, @quantity, @stock_last, @name, @staff_no, getdate())


这篇关于如何立即插入日期时间并选择当天已插入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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