Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql to linq in c# code

Posted on 2014-01-25
6
Medium Priority
?
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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