Update sql table [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to update sql table, i.e. if I have in table spicific record I want to update the record if record dosen't exist I need to add it to the table.
How can I implemet it ?
Thank you in advance.

If I understand you correctly, you want to add columns, with some values. You need to use alter query with default constraint:
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
Or if you want to check that a record or row is available in data or not and if not you want to insert that row then you need if not exist:
IF NOT EXISTS(SELECT 1 FROM emp WHERE fruits = 'mango')
INSERT INTO emp (fruits) VALUES ('mango')

Have you tried this?
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;

If you want to update a record in the database you need to get the UniqueID of the record in the table.
To do this For instance if you are going to update your personal info
Select ID, LastName, FirstName, DateofBirth from PersonalTable
If you get dataset
get the ID in Viewstate
UPDATE PersonalTable SET (LastName,FirstName,DateofBirth) = '" + xyz + "', '" + yzx + "', " + 01/01/2013 + " WHERE id= Viewstate("ID")
if you dont get use the insert query
Happy Coding

Related

How to delete only Top 100 rows which are older than 30 days from current date in C#? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
When I execute following query , it is executed properly in SSMS but same query in code does not delete anything and 0 rows are affected from thousands of rows.
Code is excuted without any error , have tried Query parameters as well but not worked.
Logs is table and DateCreated is column of DateTime.
Sql Connection settings and other things are correct.
SSMS query execution below :
Can someone please correct this query for code ?
connection.Open();
SqlCommand cmd1 = new SqlCommand("delete TOP (100) from [Logs] WHERE DateCreated < GETDATE() - 30", connection);
var number_of_rows_deleted = cmd1.ExecuteNonQuery();
result = number_of_rows_deleted.ToString() + " records deleted";
When TOP is used with DELETE, the referenced rows are not arranged in any order and the ORDER BY clause can not be directly specified in this statement. If you need to use TOP to delete rows in a meaningful chronological order, you must use TOP together with an ORDER BY clause in a subselect statement
your sql script should be like this
DELETE from [Logs]
WHERE Id IN
(SELECT TOP(100) Id
FROM Logs
WHERE DATEDIFF(day, DateCreated, GETDATE()) < 30
Order By DateCreated )

How to update comment of a specific row in SQL table with WHERE clause when its empty? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a gridview of a SQL table. The first two columns automatically get selected and put into the grid when the webpage starts and a date is selected. The third column is a comment section where I'd like to let the user input comments themselves. I have a text box that they can enter comments in when they select a row, but I can get the column to update properly.
Run_DB_Script("update Log_Transfers set Comment = '" + tmpBox.Text + "' where '" + GridView1.Rows[Row] + "'", ref tmpErr);
The bracketed [Row] is a int that is set to the row number they put in.
It executes the code, but nothing is there after hitting update.
Building a SQL statement using the input from a user is opening the door for a SQL Injection hack. A better alternative is to use parameters.
// set up the sql command to run with parameter placeholders
// parameters are prefixed with an #
var command_text = "update Log_Transfers set Comment = #comment where #grid_row";
// Define the two parameters
SqlParameter comment = new SqlParameter("#comment",tmpBox.Text);
SqlParameter grid_row = new SqlParameter("#grid_row", ....);
SqlCommand cmd = new SqlCommand();
cmd.Text = command_text;
// Add the parameters to the command
cmd.Parameters.Add(comment);
cmd.Parameters.Add(grid_row);
cmd.Execute();

Connecting Database to Registration Page asp.net [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I found people with the same problem, but none of their solutions helped me.
string checkuser = "select count(*) from Table where Username ='" + TextBox1.Text + "'";
It says "Incorrect syntax near the keyword 'Table'."
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
something about the ToString line
Is your table actually called "Table"? If so, I'd recommend choosing a different name. If you really must call it "Table", then escape it with backquotes:
select count(*) from `Table` where Username = #Username
Also, you really should not insert the raw value from your textbox into a SQL query. That makes you prone to SQL injection attacks. Instead, you should make a #Username parameter for the query and pass the value through the parameter.
If your table name is table then it is causing error because the table is use as a keyword by sql
use something like this
select count(*) from [Table] where Username = '" + TextBox1.Text + "'";

how to add instantly made rows key to another table.? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table named product where there is a field named id its the primary key here. I need to put this key to my another table pro_size_tbl as below
pro_size_tbl
==================
id
productid
sizeid
the matter is when the user adds a product the sizes of the products are saved in session and I am trying to save them in pro_size_tbl by the product key what I just made.
The thing I am doing right now is I am adding the product in the product table and in the next line I am retrieving the max id of the product table and using it as the key for the pro_size_tbl.
My question is is there any better way to do this thing?
This is simple.You can use LAST_INSERT_ID().See this sample:
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();
There is a function LAST_INSERT_ID() which can give you the desired value.
But you need to use right below the insert statement.
owh. from what i understood, u need to put auto generated mysql id to another table?
if so, here is the real deal
$sql_tbl1 = "insert here for first table";
$result_tbl1 = mysql_query($sql_tbl1) or die(mysql_error());
$id = mysql_insert_id();
$sql_tbl2 = "insert here value ($id, 'bla', 'bla')";
mysql_query(sql_tbl2);
LAST_INSERT_ID() function is ok but it is doing the same work as i am doing taking the maximum value.i guess more or less both way are the same.

How to display number of records every month according to datetime format c# asp.net [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am currently trying to display a record in my form with a sql database table called "Registration" 2 column. Information will be display in gridview
Below is the table:
Registration ID Date Registered
1 10/4/2013
2 11/4/2013
3 12/5/2013
4 13/6/2013
Result will be:
Number of record Month Registered
2 April
1 May
1 June
Do anyone know what the query is?
Try this :
Select Count(*) As NumberOfRecord, DATENAME(month, DateRegistered) As MonthRegistered
from TableName GRoup by DATENAME(month, DateRegistered)
try this
select count(*) as NumberOfRegister,DATENAME(MONTH,DateRegisterd) as MonthRegistered
from table_name
group by DATENAME(MONTH,DateRegisterd)
you can try SQL something like below to retrive data
select count(*) as Numberofrecord, datename(month, DateRegistered) as MonthRegistered
from table
group by datename(month, DateRegistered)
If you want to get records from SQL, then try something like this
SELECT MONTH(DateRegisterd) MONTH, COUNT(*) COUNT
FROM YourTableName
WHERE YEAR(DateRegisterd)=2013//If you want records of particular year
GROUP BY MONTH(DateRegisterd)
OR like this
select count(*) as Numberofrecord, datename(month, Date_Registered) as MonthRegistered
from YourTable group by datename(month, Date_Registered)

Categories