Counter in SQL and Database help!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Counter in SQL and Database help!

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    14

    Question Counter in SQL and Database help!

    Hey all!
    I'm a bit of a 'Newbie' and I'm practising SQL and Databases. I've got a windows form in VS2010 which takes a word from a text box and on a button click, that a word is stored in the database.
    I have my database called 'Setup' my table called 'tblpeople' and 2 columns called 'people' and 'occur'
    So right now my data values from textbox1 are going into 'people' like it should, but what I want to add is that if a persons name is all ready in 'people' then I want it to NOT add it in again but add a count to the 'occur' column beside it.
    so in my data base table it should look like this once i've updated it:
    people occur
    john 1
    James 5

    That's only an example since it could be any name i've entered.
    Here's my code so far:
    Code:
    private void database()
            {
                
                {
    
                    SqlConnection conn = new SqlConnection();
                    connection.ConnectionString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\John\Desktop\db\db\db\Setup.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
                    conn.Open();
    
                    SqlCommand cmd = new SqlCommand("INSERT INTO tblpeople  (people) VALUES (@ppl)", conn;
                  
    
                    try
                    {
                        string[] splin = textbox1.Text.Split(' '); 
    
                        foreach (string pstring in splin)
                        {
                           
                            if (pstring.StartsWith("*")) 
                            {
                                cmd.Parameters.AddWithValue("@ppl",pstring);
                                int rowsAffected = cmd.ExecuteNonQuery();
    
                        
                            }
                        }
                    }
    
                    finally
                    {
                        conn.Close();
                    }
                }
            }
    note: I've got the string split because it's only names starting with * to get stored.

    So any help guys? would be much appreciated.

  2. #2
    Join Date
    Mar 2012
    Posts
    14

    Re: Counter in SQL and Database help!

    bump

  3. #3
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Counter in SQL and Database help!

    Change your sql request to something like that

    IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
    UPDATE Table1 SET (...) WHERE Column1='SomeValue'
    ELSE
    INSERT INTO Table1 VALUES (...)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center