Link to home
Start Free TrialLog in
Avatar of loveuajay
loveuajay

asked on

sql to linq in c# code

cn.Open();
            SqlCommand cmsql;
            SqlDataReader drsql;


            string sql = "select max(patientid)as patientid from patient";



            cmsql = new SqlCommand(sql, cn);

            drsql = cmsql.ExecuteReader();
            if (drsql.Read())
            {
         
                    int pid = Convert.ToInt16(drsql["patientid"].ToString()) + 1;
                    txtpatientid.Text = pid.ToString();
                

            }


            else
            {

                txtpatientid.Text = "1";
            }
            cn.Close();
            cmsql.Dispose();
            drsql.Close();
       

Open in new window


this is my sql code how i make it in linq


my table name is== patient
Avatar of Habib Pourfard
Habib Pourfard
Flag of New Zealand image

using (MyDataContext dc = new MyDataContext())
{
    int pid = dc.patients.Max(t => t.patientid);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of loveuajay
loveuajay

ASKER

i get this error

Error      1      The type or namespace name 'DataContextType' could not be found (are you missing a using directive or an assembly reference?)      D:\ajay\jolly tech project\jollytech p1\jollytech p1\patient.cs      79      26      jollytech p1
Hi loveuajay;

When working with Linq to SQL the first thing you need to do is create your data context and in Linq to SQL it is creating a EDMX which is Object Relational/Model, This model defines the tables in the database and the relationships between them. I would suggest reading the following web link which describe what LinQ to SQL is and how to create the OR/M, EDMX file and querying the database.

Using Linq to SQL Part 1

Once you have created the EDMX file which maps the database to the classes in your code that is what you will use for the variable DataContextType in the code snippet I posted.
thanks i get it
Not a problem, glad to help.