Insert Current Time of SQL server to database thr ASP.net

Discussion in 'ASP .Net' started by Guest, Feb 19, 2005.

  1. Guest

    Guest Guest

    I have a datetime field in table.

    I want to insert the current time of database server to the table thr
    ASP.NET (C#).

    I have dataset to do the insert:

    DataSet1.TableNameRow rowNew =
    (DataSet1.TableNameRow)DataSet1.TableName.NewRow();
    rowNew.DateTimeField = System.DateTime.Now;
    (rest of the code..)

    I would like to ask in the second line of the code, I can insert the current
    time of the web server to the DateTimeField,
    However if I want the time refer to the current time of the SQL server, i.e
    the GETDATE() of MS SQL , what should I write?

    I can't type

    rowNew.DateTimeField = "GETDATE()";

    Thanks for any help.
     
    Guest, Feb 19, 2005
    #1
    1. Advertising

  2. The Dataset is not meant to be used to update in this way. First of all, the
    dataset don't have a clue what SQL Server is, therefore, GETDATE() will not
    work. Secondly, this is not the most effecient way to do an update.
    Insert this date using the SqlConnection and SqlCommand objects while
    constructing your sql string.

    "" wrote:

    > I have a datetime field in table.
    >
    > I want to insert the current time of database server to the table thr
    > ASP.NET (C#).
    >
    > I have dataset to do the insert:
    >
    > DataSet1.TableNameRow rowNew =
    > (DataSet1.TableNameRow)DataSet1.TableName.NewRow();
    > rowNew.DateTimeField = System.DateTime.Now;
    > (rest of the code..)
    >
    > I would like to ask in the second line of the code, I can insert the current
    > time of the web server to the DateTimeField,
    > However if I want the time refer to the current time of the SQL server, i.e
    > the GETDATE() of MS SQL , what should I write?
    >
    > I can't type
    >
    > rowNew.DateTimeField = "GETDATE()";
    >
    > Thanks for any help.
    >
    >
    >
    >
     
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=, Feb 19, 2005
    #2
    1. Advertising

  3. Guest

    IPGrunt Guest

    On 19 Feb 2005, "=?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?="
    <> postulated in
    news::

    > The Dataset is not meant to be used to update in this way. First

    of all, the
    > dataset don't have a clue what SQL Server is, therefore, GETDATE()

    will not
    > work. Secondly, this is not the most effecient way to do an

    update.
    > Insert this date using the SqlConnection and SqlCommand objects

    while
    > constructing your sql string.
    >
    > "" wrote:
    >
    >> I have a datetime field in table.
    >>
    >> I want to insert the current time of database server to the table

    thr
    >> ASP.NET (C#).
    >>
    >> I have dataset to do the insert:
    >>
    >> DataSet1.TableNameRow rowNew =
    >> (DataSet1.TableNameRow)DataSet1.TableName.NewRow();
    >> rowNew.DateTimeField = System.DateTime.Now;
    >> (rest of the code..)
    >>
    >> I would like to ask in the second line of the code, I can insert

    the current
    >> time of the web server to the DateTimeField,
    >> However if I want the time refer to the current time of the SQL

    server, i.e
    >> the GETDATE() of MS SQL , what should I write?
    >>
    >> I can't type
    >>
    >> rowNew.DateTimeField = "GETDATE()";
    >>
    >> Thanks for any help.
    >>
    >>
    >>
    >>


    You cannot directly access SQL server batches in an ASP.NET script.

    You must interface to SQL server through ADO.NET, which provides
    classes, like SqlConnection and SqlCommand that provide access to the
    data server.

    Look at some of the examples describing server-side data access in
    the ASP.NET examples:

    http://samples.gotdotnet.com/quickstart/aspplus/


    -- ipgrunt
     
    IPGrunt, Feb 19, 2005
    #3
  4. Typically, you wouldn't do this in .NET. You'd do it in your database.
    Just set the default value for your datetime column to getdate()

    Then, just don't reference the column in your insert statement.

    If you are using stored procedures, you'd just update the column
    with getdate() on update calls.

    --
    2005 Microsoft MVP C#
    Robbe Morris
    http://www.robbemorris.com
    http://www.mastervb.net/home/ng/forumvbcode/post10017013.aspx
    http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp



    <> wrote in message news:...
    >I have a datetime field in table.
    >
    > I want to insert the current time of database server to the table thr
    > ASP.NET (C#).
    >
    > I have dataset to do the insert:
    >
    > DataSet1.TableNameRow rowNew =
    > (DataSet1.TableNameRow)DataSet1.TableName.NewRow();
    > rowNew.DateTimeField = System.DateTime.Now;
    > (rest of the code..)
    >
    > I would like to ask in the second line of the code, I can insert the
    > current
    > time of the web server to the DateTimeField,
    > However if I want the time refer to the current time of the SQL server,
    > i.e
    > the GETDATE() of MS SQL , what should I write?
    >
    > I can't type
    >
    > rowNew.DateTimeField = "GETDATE()";
    >
    > Thanks for any help.
    >
    >
    >
     
    Robbe Morris [C# MVP], Feb 21, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sham Yemul
    Replies:
    3
    Views:
    578
    William Ryan eMVP
    Mar 3, 2004
  2. Replies:
    1
    Views:
    1,764
    Jan Peter Stotz
    Jun 8, 2005
  3. =?Utf-8?B?QmVubnk=?=
    Replies:
    5
    Views:
    654
    Steven Cheng[MSFT]
    May 1, 2006
  4. Lorenzo

    Walking thr DataReader

    Lorenzo, Dec 18, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    161
    JDP@Work
    Dec 20, 2005
  5. Replies:
    0
    Views:
    91
Loading...

Share This Page