0

I want to work with SQL scripts in my desktop application. But I can not write any sql statements with parameters and call them.

INSERT INTO table 
VALUES (..., ..., ...) 

Above should my SQL statement; where I want to fill the values at c# side. How should my SQL statement; what is its notation? And how can I add at c# parameters to it.

Thanks !

2 Answers 2

4

You're going to want to use parameterized SQL queries. Here's an example using the SqlParameter class.

Sign up to request clarification or add additional context in comments.

2 Comments

I know this. Thanks but this is not the answer of my question. I want to use an sql-script file, your answer is using directly a string as command text.
wtf? his answer is very much correct! whether your sql-statement is written directly in code or is being read from some text files doesn't matter.
1

If you need to use external sql script files you can use SQLCMD, a command line tool available for SQL Server 2005 or later.

You can just trigger sqlcmd with Process.Start from C# and pass it the necessary parameters. Here's an article explaining how to pass in variables for starters.

If you are just looking for table update/inserts though I'd recommend going with SqlCommand/SqlParameters as wsanville suggested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.