Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Register
Login
Frame SQL query using String formatters
WhatsApp
Hemant Kumar
12y
2.5
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.
String
Formatters