| |

VerySource

 Forgot password?
 Register
Search
View: 704|Reply: 6

Judge the number of data in the database to update the number without inserting data? ?

[Copy link]

4

Threads

24

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-2-11 00:00:01
| Show all posts |Read mode
I am writing a popular search`
The idea is to use a SQL table to store search words that the user can find with a result greater than 1.
But one problem is that if the keyword already exists in the database, then it will not be repeated?
My code is written like this:

        int newcount = 1;
        try
        {
            SqlCommand upcount = new SqlCommand ("update aspnet_HotSearch set Count = Count + 1 Where KeyWord = '" + key + "'", mysqlSqlConnection);
            upcount.ExecuteNonQuery ();
        }
        catch
        {
            SqlCommand intoKey = new SqlCommand ("insert into aspnet_Hot (Key, Count) value ('" + key + "', '" + newcount + "')", mysqlSqlConnection);
            intoKey.ExecuteNonQuery ();
        }

But it's useless, so please tell me how to write
By the way, are there any better ideas for popular searches?
Reply

Use magic Report

0

Threads

29

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-4-7 03:00:02
| Show all posts
Write as a stored procedure, judge in the stored procedure
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-7 14:15:01
| Show all posts
How can you use try ... catch to judge, don't you be efficient?
Write insert and update as methods separately. Use IF to determine which to call
Reply

Use magic Report

4

Threads

24

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

 Author| Post time: 2020-4-8 02:15:02
| Show all posts
Oops` I'm stupid ...

Can you write an example `
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-8 11:15:01
| Show all posts
lz write a trigger to check whether the data to be inserted already exists at each insertion, and update the record if it exists
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-9 16:15:01
| Show all posts
Write a stored procedure, use the return value of the stored procedure to determine whether to update or insert
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-9 19:45:01
| Show all posts
Write 3 methods: find, update, insert
First use the search to determine whether it exists or not, generally it will return -1 if it does not exist, and it will return the number of found;
Then use
if (num ==-1) {insert;} else {update;}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list