Solved

sql to linq in c# code

Posted on 2014-01-25
6
491 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 63

Accepted Solution

by:
Fernando Soto earned 500 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 63

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 63

Expert Comment

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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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