Solved

sql to linq in c# code

Posted on 2014-01-25
6
484 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 62

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 62

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 62

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now