TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Frame SQL query using String formatters
Hemant Kumar
Dec 21 2011
Resource
0
0
3.1
k
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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 formore about the Str
ing
Format
funct
ion.
String
Formatters