Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Calvin Frei 106 posts 314 karma points
    May 18, 2010 @ 23:55
    Calvin Frei
    0

    How can I check if a value in a table already exist?

    Hi Umbracians

    I use this code to insert values

    Node

     

     

    n = Node.GetCurrent();

     

    ISqlHelper

     

     

    SqlHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);

    SqlHelper.ExecuteNonQuery(

     

     

    @"INSERT INTO mytable(TEST,NODEID)

    values(@TEST,@NODEID)",

    SqlHelper.CreateParameter(

     

    "@TEST", test),

    SqlHelper.CreateParameter("@NODEID", n.Id));

    and this code to update

     

     

    ISqlHelper SqlUpdateHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);

    SqlUpdateHelper.ExecuteNonQuery(

     

     

    @"UPDATE mytable SET...

    but that can't work, because it insert and update the values in the database at the same time :-).

    How can I check if n.Id in mytable already exist?

    I think of something like that

     

     

    if (value... @NODEID = n.Id) {

    UPDATE mytable SET...

    }

     

     

    else

    INSERT INTO mytable...

    Calvin

  • Aaron Powell 1708 posts 3046 karma points c-trib
    May 19, 2010 @ 01:08
    Aaron Powell
    0

    I suggest you use a real ORM (like LINQ to SQL) to interact with your own database rather than the Umbraco data layer, unless you really love writing your own TSQL statements :P.

    If you really want to write TSQL you'll need to execute a scalar select query on your node ID, check if it returned something and then have an IF conditional to determine whether to do an Update or Insert.

Please Sign in or register to post replies

Write your reply to:

Draft