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 + "'";
Related
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 )
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();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So, I'm making a program in WindowsForm with C# and I have a Search Box, where the user can search a list of clients.
What I want to do is: when the user hits 'Search', select from the clients table all the lines that contains, in the name column, the text that the user had written. Like:
I have Alan and Mark registred in the clients table, and the user writes 'a' in the searchbox, it has to bring Alan and Mark, because they have the letter 'a' in their name.
string userInput = searchTextBox.Text;
string query = "select* from client where name like" + '%' + userInput + '%';
Now you can use that query.
[EDIT FOR SECURITY]
cmd.CommandText = "select* from client where name like #Name;";
cmd.Parameters.AddWithValue("#Name", "%" + userInput + "%");
A simple like will solve your problem:
Select * from yourtable where [name] like '%a%'
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to write a C# application to check the selected stored procedure for some criteria. For example if the stored procedure contains 5 select queries the same query must contais 5 with(nolock) (select for temp tables except).
How can I do this via C#? Thanks in advance.
You can use sys.objects to query SQL Server's metadata in order to analyze SP's definition like a text. In your case you can create query such a listed below and check its results from C# app:
SELECT object_definition(object_id) as [sp definition]
, schema_name(schema_id) [schema]
, name
, type_desc
FROM sys.objects
where object_definition(object_id) like '%select%select%select%'
and type_desc = 'SQL_STORED_PROCEDURE'
OR
SELECT object_definition(object_id) as [sp definition]
, schema_name(schema_id) [schema]
, name
, type_desc
FROM sys.objects
where object_definition(object_id) like '%NOLOCK%'
and type_desc = 'SQL_STORED_PROCEDURE'
Not sure what the question is.
You obviously need to parse the SQL (because NOLOCK can also be in a comment).
And to get the source of a stored procedure - well, use something like
using (SqlConnection sqlConnection = new SqlConnection())
{
sqlConnection.ConnectionString = yourConnectionStringHere;
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand("sys.sp_helptext", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("#objname", "stored_proc_name_here");
DataSet ds = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(ds);
return DataTableToString(ds.Tables[0]);;
}
although I would never touch the sql server because I keep the source outside in version control, so a Visual Studio plug in would be the better solution.
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