Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql to linq in c# code

Posted on 2014-01-25
6
Medium Priority
?
498 Views
Last Modified: 2016-02-10
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
0
Comment
Question by:loveuajay
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 39808691
using (MyDataContext dc = new MyDataContext())
{
    int pid = dc.patients.Max(t => t.patientid);
}

Open in new window

0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39808754
Hi loveuajay;

The following code snippet will do the same as your SQL code in the question.

// Create a Linq to SQL DataContext 
var db = new DataContextType();
Int16 maxPatientId = 0;

// Get the max ID from the patient table 
if( db.patient.Count() != 0 )
    maxPatientId = (Int16) db.patient.Max( p => p.patientid );

txtpatientid.Text = (++maxPatientId).ToString();

Open in new window

0
 

Author Comment

by:loveuajay
ID: 39809691
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
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39810106
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.
0
 

Author Closing Comment

by:loveuajay
ID: 39811550
thanks i get it
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39812068
Not a problem, glad to help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question