Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Frame SQL query using String formatters
WhatsApp
Hemant Kumar
13y
3.1
k
0
5
Resource
0
The string.Format method is a static method that receives a string that specifies where the following arguments should be inserted, and these are called substitutions.
In source code we need to use to frame the SQL Query
public
bool
SaveData(
string
firstName,
string
lastName)
{
String
connectionString = ConfigurationManager.
ConnectionStrings
[
"TESTDB"
].
ConnectionString
;
bool
result =
false
;
using
(
SqlConnection
connection =
new
SqlConnection
(connectionString))
{
SqlCommand
cmd =
new
SqlCommand
();
cmd.
Connection
= connection;
cmd.
CommandText
=
String
.
Format
(
"insert into
Test
_DB.dbo.PersonName(FirstName,LastName) values ('{0}','{1}')"
, firstName, lastName);
cmd.
CommandType
=
CommandType
.
Text
;
connection.
Open
();
int
count = cmd.
ExecuteNonQuery
();
if
(count > 0)
{
result =
true
;
}
else
{
result =
false
;
}
}
return
result;
}
Like thi
s we can frame the Update and Delete Statements also. Please refer this link for more about the Str
ing
Format
funct
ion.
Formatters
String