SQL表中的数据添加和更新 [英] Data addition and updation in SQL tables

查看:79
本文介绍了SQL表中的数据添加和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于SQLClient和其他所有用户来说,我还是很陌生,而且我的SQL表存在问题..每当我运行我的代码时,数据(而不是进行更新)都会将自身附加到表中已存在的记录上.我的代码



Iam fairly new to SQLClient and all, and iam having a problem with my SQL tables..when ever i run my code, the data, rather than getting updated, attaches itself to the already existing records in the tables..here''s my code



SqlConnection conneciones = new SqlConnection(connectionString);

SqlCommand cmd;

conneciones.Open();





int leak = 0;

            //put values into SQL DATABASE Table 1
            for (int ok = 0; ok < CleanedURLlist.Length; ok++)
            {
                leak = ok;

                cmd = new SqlCommand("insert into URL_Entries values('" + CleanedURLlist[ok] + "' , '" + DateTime.Now + "' , '" + leak + "' )", conneciones);

                cmd.ExecuteNonQuery();
            }







conneciones.Dispose();

推荐答案

首先,这是一些SQL帮助: ^ ],以帮助了解Update语句与Insert语句的区别.

其次,建议对SqlCommand使用参数化查询,而不要按照自己的方式进行.参数化查询可以使您的查询免受SQL Injection攻击.这方面有一些帮助:参数化查询 [
希望这对您有帮助...

欢呼
First off, here is some SQL help: SQL tutorials[^] to help with how Update statement is different from Insert statement.

Second, the use of parameterised queries is recommened for SqlCommand rather than doing it the way you are doing. Parameterised queries significantly make your queries protected against SQL Injection attacks. Here is some help on that: Parametrised Queries[^]

Finally, you may be new to "SqlClient and all" but you are certainly not new to Googling...all this basic information is all over the internet, you can also download e-books for C# and ADO.NET that go over these concepts at great length.

Hope this all helps...

Cheers


对不起,队友,看来您不仅是SqlClient的新手,还是SQL的新手.我建议阅读UPDATE和INSERT的工作原理,看看如何使用SqlCommand进行UPDATE和INSERT的示例代码.这不是教程部分,而是快速解答"部分.我已经解释了很多.但是我会用简单的英语给你一些关于你应该做什么的算法:

I am sorry mate, it seems you are not only new to SqlClient but also totally new to SQL. I would recommend reading on how UPDATE and INSERT work, have a look around for sample code as to how to do UPDATE and INSERT using SqlCommand. This is not a tutorial section rather a "Quick Answer" section. I have already explained a lot. But i will give you some algorithm in plain English as to what you should be doing:

--get the record count from the table

--if (count == 0)
----execute the INSERT command
--else
----execute the UPDATE command



还有两点

1)就像对UPDATE查询一样,对INSERT查询进行参数设置.

2)而不是执行SELECT * FROM< tablename> ;,而是执行SELECT COUNT(*)FROM< tablename> ;,这将直接为您提供记录计数,而不是加载整个表.除非,您还需要表数据.

这比任何人都可以在这里帮助您争取到一点更重要.您的代码没有错,如上所述,它只需要重新排列即可.

干杯...



Also, a couple of points

1) parameterise the INSERT query too like you did for the UPDATE query.

2) instead of doing a SELECT * FROM <tablename>, do a SELECT COUNT(*) FROM <tablename>, this will just give you the record count directly rather than loading the whole table. Unless, you need the table data as well.

That''s more than anyone will help you here trying to get one point across. Your code is not wrong, it just needs some rearranging as I mentioned above.

Cheers...


您正在运行插入查询.插入查询将新记录添加到表中.
而是在循环中运行更新查询.这将更新已经存在的记录.
You are running an insert query. An insert query will add new records to the table.
Run an update query in your loop instead. This will update records that already exist.


这篇关于SQL表中的数据添加和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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