Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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



C#
SqlConnection conneciones = new SqlConnection(connectionString);

SqlCommand cmd;

conneciones.Open();


C#
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();
            }



C#
conneciones.Dispose();
Posted

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
 
Share this answer
 
Comments
RaviRanjanKr 16-Oct-11 17:41pm    
My 5+
I.explore.code 17-Oct-11 3:53am    
Thanks. :)
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:

PHP
--get the record count from the table

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


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...
 
Share this answer
 
Comments
RaviRanjanKr 16-Oct-11 17:42pm    
My 5+
I.explore.code 17-Oct-11 3:54am    
Thanks :)
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.
 
Share this answer
 
Comments
Shahvez Irfan 15-Oct-11 10:35am    
like "update URL_Entries values(...)" ??
I reformed the code like this..but it gives me exceptions..can't seem to figure out what i've done wrong in this


C#
for (int ok = 0; ok < CleanedURLlist.Length; ok++)
           {
               leak = ok;



              // cmd = new SqlCommand("UPDATE URL_Entries SET('" + CleanedURLlist[ok] + "' , '" + DateTime.Now + "' , '" + leak + "' )", conneciones);



               cmd = new SqlCommand("UPDATE URL_Entries SET URL=@NewURL, Date=@NewDate" + "WHERE Crawl_ID=@NewCrawl_ID", conneciones);

               cmd.Parameters.AddWithValue("@Crawl_ID", ok);
               cmd.Parameters.AddWithValue("@URL", CleanedURLlist[ok]);
               cmd.Parameters.AddWithValue("@Date", DateTime.Now);


              cmd.ExecuteNonQuery();

               //cmd.ExecuteNonQuery();
           }
 
Share this answer
 
Comments
André Kraak 15-Oct-11 11:38am    
The names of the parameters names (@...) need to be the same in both the UPDATE statement and AddWithValue function.
Shahvez Irfan 15-Oct-11 11:43am    
that gives me an exception: Must declare the scalar variable "@NewDateWHERE".
André Kraak 15-Oct-11 12:31pm    
You need to add a space between the parameter name and the WHERE.
In fact you can use:
cmd = new SqlCommand("UPDATE URL_Entries SET URL=@NewURL, Date=@NewDate WHERE Crawl_ID=@NewCrawl_ID", conneciones);
ah! i see, there needs to be a space between "@NewDate" and "Where"...and also what Andre mentioned above...

Here you go:

C#
for (int ok = 0; ok < CleanedURLlist.Length; ok++)
           {
               leak = ok;



              // cmd = new SqlCommand("UPDATE URL_Entries SET('" + CleanedURLlist[ok] + "' , '" + DateTime.Now + "' , '" + leak + "' )", conneciones);



               cmd = new SqlCommand("UPDATE URL_Entries SET URL=@NewURL, Date=@NewDate " + "WHERE Crawl_ID=@NewCrawl_ID", conneciones);

               cmd.Parameters.AddWithValue("@NewCrawl_ID", ok);
               cmd.Parameters.AddWithValue("@NewURL", CleanedURLlist[ok]);
               cmd.Parameters.AddWithValue("@NewDate", DateTime.Now);


              cmd.ExecuteNonQuery();

               //cmd.ExecuteNonQuery();           
}
 
Share this answer
 
v3
Comments
Shahvez Irfan 15-Oct-11 14:16pm    
Thank you, lad! it works now..the only issue is that iam not getting any values in my table like i was with the Insert command...i open the table and i get null values..any idea there?
I.explore.code 15-Oct-11 16:44pm    
does your table already contain any record with the CrawlID you are using in the code? if not, then that might be your problem coz Update would work only when there is an existing record with the same ID in your table. Have a go...
Shahvez Irfan 15-Oct-11 17:47pm    
yeah, you're right, but what if my table is empty? the insert query appends the same data in rows when the code is run the second time..how do i use update to first populate the table with the values, and then when i run the code again with the updated values, it should replace the values initially inserted?
I.explore.code 15-Oct-11 18:28pm    
mate, first you have got to INSERT data if your table is empty, UPDATE cannot work on empty tables because it would not have any IDs to update against. So, first populate your tables using INSERT statement and then update it using UPDATE. Its that simple. You can write code to get the record count from the table, if the count > 0 then execute the update code else execute insert code. I really hope this makes it clearer :)
Shahvez Irfan 15-Oct-11 20:34pm    
Got it! thanks, lad..that was helpful
i have a question though? can the UPDATE query work on all the entries of the tables? mine apparently replaces the all previously INSERTED rows with the same value. the code is as follows in the solution box:-
UPDATE query replaces all the initially inserted values with the same entry..specifically the last entry of the table..how do i get it to replace with new values for every entry??


C#
for (int ok1 = 0; ok1 < CleanedURLlist.Length; ok1++)
           {




               // cmd = new SqlCommand("UPDATE URL_Entries SET('" + CleanedURLlist[ok] + "' , '" + DateTime.Now + "' , '" + leak + "' )", conneciones);

               cmd = new SqlCommand("UPDATE URL_Entries SET URL=@NewURL, Date=@NewDate WHERE Crawl_ID=@NewCrawl_ID", conneciones);


               cmd.Parameters.AddWithValue("@NewURL", CleanedURLlist[ok1]);
               cmd.Parameters.AddWithValue("@NewDate", DateTime.Now);
               cmd.Parameters.AddWithValue("@NewCrawl_ID", ok1);


               cmd.ExecuteNonQuery();

               //cmd.ExecuteNonQuery();
           }


           cmd.CommandText = "SELECT * FROM URL_Entries";

           SqlDataReader ReadTable = cmd.ExecuteReader();

           table.Load(ReadTable);

           int jacky = table.Rows.Count;




           //INSERT or UPDATE VALUES IN DATABASE

           if (jacky == 0)
           {
               for (int ok = 0; ok < CleanedURLlist.Length; ok++)
               {


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

                   cmd.ExecuteNonQuery();
               }
           }//end of IF
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900