Write Inner Join in inline SQL Query in C# and VB.Net

Last Reply 6 months ago By pandeyism

Posted 6 months ago

i have a query:

SELECT l.company,l.email,e.event_name 
FROM exb_login l
inner join crm_event e on l.event_id=e.event_id  
WHERE  user_id=?user_id 

i wanted to include inner join into this:

string query1 = "SELECT company,email FROM   exb_login  l   " +
                " WHERE  user_id=?user_id  ";

How to do this?

You are viewing reply posted by: pandeyism 6 months ago.
Posted 6 months ago

Hi chetan,

Refer below code.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
    {
        string query = "SELECT O.OrderId, O.ShippedDate, C.CompanyName, C.City FROM Orders O INNER JOIN Customers C ON O.CustomerId = C.CustomerId WHERE OrderId = @OrderId";
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@OrderId", 10643);
            con.Open();
            cmd.ExecuteReader();
            con.Close();
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
        Dim query As String = "SELECT O.OrderId, O.ShippedDate, C.CompanyName, C.City FROM Orders O INNER JOIN Customers C ON O.CustomerId = C.CustomerId WHERE OrderId = @OrderId"

        Using cmd As SqlCommand = New SqlCommand(query, con)
            cmd.Parameters.AddWithValue("@OrderId", 10643)
            con.Open()
            cmd.ExecuteReader()
            con.Close()
        End Using
    End Using
End Sub