Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Please guide how to convert the below code to corresponding entity framework code [considering     dbempEntities obj = new dbempEntities ();]

            SqlConnection con = new SqlConnection("Data Source=111.178.0.113;Initial Catalog=test;Uid=test;Pwd=111;");
            SqlCommand cmd = new SqlCommand("Select Count(*) from tblemptemp where Name='" + tm.Name + "'", con);
            con.Open();
            object result = cmd.ExecuteScalar();
            con.Close();
            if (result != null)
            {
                IsEmployeeExists = Convert.ToInt32(result.ToString()) > 0;
            }
Posted
Comments
Suvendu Shekhar Giri 5-Dec-15 7:34am    
.. and what is your question?
Are you getting any error?

Note: Never post sensitive data over web like UserId, Password etc.
Anisuzzaman Sumon 8-Dec-15 6:45am    
You may use LINQ instead of
Richard Deeming 9-Dec-15 11:01am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

Equvalent linq code of "SELECT COUNT(*) FROM ToDos WHERE Name='ABC'" will be
considering
C#
dbempEntities obj = new dbempEntities ();


C#
var count = (from todo in obj.ToDos
                         where todo.Name=="ABC"
                         select obj.ToDos.AsEnumerable()
                       
                      ).Count();


here ToDos is my table name contained by entities yours 'tblemptemp'
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900