Developer Network
Developer Network
Developer Network
ProfileText
ProfileText
:CreateViewProfileText:
Sign in
Subscriber portal
Get tools
Downloads
Visual Studio
SDKs
Trial software
Free downloads
Office resources
Programs
Subscriptions
Overview
Administrators
Students
Microsoft Imagine
Microsoft Student Partners
ISV
Startups
Events
Community
Magazine
Forums
Blogs
Channel 9
Documentation
APIs and reference
Dev centers
Samples
Retired content
We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second.
This forum has migrated to
Microsoft Q&A
.
Visit
Microsoft Q&A
to post new questions.
Learn More
Ask a question
Quick access
Forums home
Browse forums users
FAQ
Search related threads
Remove From My Forums
Answered by:
check if record exists in SQL database ?
Archived Forums 421-440
>
Visual C#
Question
0
Sign in to vote
Im trying to check if a record in database already exists.
SqlConnection cnn =
new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand ???
cnn.Open();
cmd.ExecuteNonQuery();
Friday, November 7, 2008 9:57 AM
Answers
1
Sign in to vote
You have to customize the sql to fit your needs. Also it is good to rewrite it and use parameters there.
1
public
static
bool
isRecordExists(
object
myObject )
2
{
3
bool
result =
false
;
4
5
SqlConnection conn =
new
SqlConnection( connString );
6
7
try
8
{
9
string
sql=
"SELECT * FROM MyTable WHERE FirstName='"
+ myObject.FirstName +
"' ADN LastName='"
+ myObject.LastName +
"'"
;
10
11
SqlCommand cmd =
new
SqlCommand( sql, conn );
12
SqlbDataReader reader = cmd.ExecuteReader();
13
14
if
( reader.Read() )
15
{
16
result =
true
;
17
}
18
reader.Close();
19
}
20
finally
21
{
22
db.complete();
23
}
24
25
return
result;
26
}
Proposed as answer by
Harry Zhu
Tuesday, November 11, 2008 6:33 AM
Marked as answer by
Harry Zhu
Wednesday, November 12, 2008 9:47 AM
Friday, November 7, 2008 11:35 AM