Update Sql Table By Listview Items - c#

My listview has three columns and rows as below:
Trans ID Name Amount
1 mahesh 1000
2 mahendra 2000
3 kirti 3000
Is there possible to update/insert/delete on sql table like below:
Delete from mytable where transID = 3
Update mytable set name = ‘mahendra’ where transID=2
Insert into mytable(transID,name,amount)values(#transID,#name,#amount)
Is it possible The above three sql transact statement with Listview?.
I am Looking For Code Example Here.

If you mean is update the DB Table by your listview ...
I think it is like ↓
1) delete from yourtable where transid in (all ids in your listview);
2) foreach(var item in listview.Items) Insert into yourtable(transID,name,amount)values(#transID,#name,#amount)

Related

Create an id autoincrement with dapper

I'm working with Dapper and .Net 6.0... I have to do an insert from table1 to table2... in the insert the columns match each other... the only column is "toID" of table1... which does NOT match the column of table2 (that's why I put a 4 in it) but I have to make it auto-increment so that for each insert there is an incrementing sequence
var sql =
$"INSERT INTO table1 (toId,teId,dateShift,SectorOrigen) SELECT 4,teID,#dateModify,#LastSector FROM table2";
That is to say... that when I generate an insert the ID = 1, then another ID = 2 and so on continuously
Any advice??
You can create and use the sequence https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver16

how to update the selected rows in same query?

I have 1 table table1, and a select query
select col1 from table1 where id <= 10;
I want to update the same records
update table1 set col2 = 'some value' where id < 10;
I want to do both the operations in 1 query and get the selected result also so that i can use it.
How can I do that?
You can not update and select row using one single query, since both are different operation.
So you need to write 2 different queries.
If you don't need to update table and want temporary output, then #Yosi had provided you with perfect solution.
You don't have to update it, you an use CASE statement:
SELECT col2 = CASE WHEN id<=10 THEN 'some value' ELSE col1 END
FROM table1
Try this sql query using OUTPUT to get the updated vlaue
UPDATE persons
SET LastName = 'test'
OUTPUT Inserted.LastName
where id<10;
If you need the old value after updation:
UPDATE persons
SET LastName = 'test'
OUTPUT DELETED.LastName
where id<10;
DEMO

Insert Multiple Rows to table after select and change Values rows

:)
i have a table the Includ any row.
Names Rows my Table is:
ID_ImgPhoto(PK),IDPhoto(FK),Title,Small,Larg,AddedDate,AddedIp,Status.
i want read at the DataReader in asp.net and change string the String.replace() many Values Small And Larg row in my table.
i want Insert Multiple Rows to table after select and change The Same Values rows.
i want select many row With a Condition.
My select Code Is:
ALTER Procedure [dbo].[sp_Tbl_ImagePhotoGraphy_SelectRow]
#IDPhoto int
As
Begin
Select
[ID_ImgPhoto],
[IDPhoto],
[Title],
[Small],
[Larg],
[AddedDate],
[AddedIp],
[Status]
From Tbl_ImagePhotoGraphy
Where
[IDPhoto] = #IDPhoto
End
i Want changed value many rows after the give Value #IDPhoto And select Many Row my table and FOR EXAMPLE :
/*OLD DATA*/
ID_ImgPhoto IDPhoto Small Larg
1 1 OLD/1S.jpg OLD/1L.jpg
2 1 OLD/2S.jpg OLD/2L.jpg
3 1 OLD/3S.jpg OLD/3L.jpg
4 2 Oldsmall/1S.jpg OldLarg/1L.jpg
5 2 Oldsmall/1S.jpg OldLarg/1L.jpg
5 2 Oldsmall/1S.jpg OldLarg/1L.jpg
5 2 Oldsmall/1S.jpg OldLarg/1L.jpg
____________________________________________________________________
/*NEW DATA Afetr Select And Chnage Rows Value*/
ID_ImgPhoto IDPhoto Small Larg
1 1 NEW/1S.jpg NEW/1L.jpg
2 1 NEW/2S.jpg NEW/2L.jpg
3 1 NEW/3S.jpg NEW/3L.jpg
4 2 Oldsmall-2/1S.jpg OldLarg-2/1L.jpg
5 2 Oldsmall-2/1S.jpg OldLarg-2/1L.jpg
5 2 Oldsmall-2/1S.jpg OldLarg-2/1L.jpg
5 2 Oldsmall-2/1S.jpg OldLarg-2/1L.jpg
I FIND THIS Sulotion.:)
UPDATE yourtable set small=replace(small,'OLD/','NEW/'),larg=replace(larg,'OLD/','NEW/')

How to Filter dataset for two column

I have datatable which is binded with grid as following
Table1
Id Type Desc
1 A ABC
2 A XYZ
1 B QRS
3 B 123
Based upon the user selection on Grid row following table is generating.
Table2
Id Type
1 A
1 B
2 A
This second table can grow till first table.
I have to find out filtered rows from table 1 based upon selection of grid (or output as table2)
I have following questions
Should i create second table2 from rows selection of grid to filter table1
If yes, then how to filter with these two table
If No answer of question1 then what is best way.
I am using dEV eX GRID.
you can use a dataview and set correctly the RowFilter property to filter your first datable
with filter value of second table,for instance:
dataView.RowFilter = "Id IN (1, 2) AND Type = A"
for sure you have to make it dynamic so you can scan each row and
make your rowFilter that is id = 1 and type = A or id = 2 and type = A etc...
here a link to understand how use it:
http://www.csharp-examples.net/dataview-rowfilter/

How do I delete all but the newest 20 rows in a table?

How do you delete the top row? I want to delete the top row if theres more than 20 rows.
im using c#.
Have a look at DELETE (Transact-SQL)
TOP (expression) [ PERCENT ]
Specifies the number or percent of
random rows that will be deleted.
expression can be either a number or a
percent of the rows. The rows
referenced in the TOP expression used
with INSERT, UPDATE, or DELETE are not
arranged in any order.
Maybe also have a read at The DELETE statement in SQL Server
Some info about deletion http://msdn.microsoft.com/en-us/library/bb386925.aspx
var ordFetch =
(from ofetch in db.Orders
where ofetch.OrderID == reqOrder
select ofetch).First();
db.Orders.DeleteOnSubmit(ordFetch);
db.SubmitChanges();
To select top rows:
var data = (from p in people
select p).Take(100);
Assuming the ID field is a suitable substitute and always increases with time (i.e., smallest values of ID are the oldest, and that is always the case):
using (SqlConnection conn = new SqlConnection("yourconnectionstring"))
{
SqlCommand cmd = conn.CreateCommand(
"DELETE FROM table WHERE ID NOT IN (SELECT TOP 20 ID FROM table ORDER BY ID DESC)");
cmd.ExecuteNonQuery();
}
(My C# syntax may be slightly off -- I did this from memory -- but the SQL query should be fine.)

Categories

Resources