开发者

Combine 2 SQL queries into one query and convert to C#

I need to combine these two separate queries into one because it's not efficient AND have this converted to C# ASP.NET from Classic ASP

SQL = "SELECT MemID FROM network WHERE userid='"&strTemp&"'"
Set rsNet = Conn.Execute(SQL)
if NOT rsNet.eof then
strList = rsNet("memID")
end if
rsNet.close
set rsNet = Nothing

SQL = "SELECT Distinct userid, username, img1, birthday, gendid, city, state, country, title FROM UserIn开发者_JAVA百科fo WHERE (userinfo.userid IN (" & strList & ")) "
Set rsView = Server.CreateObject("ADODB.Recordset")
rsView.Open SQL, Conn, adOpenKeyset, adLockReadOnly
if NOT rsView.EOF then
arrN = rsView.getrows()
end if
rsView.close
set rsview = nothing


I don't know about the C#, but the SQL goes to a classic join...

SELECT userid, username, img1, birthday, gendid, city, state, country, title
FROM UserInfo
JOIN network
ON network.MemID = userinfo.userid
AND network.userid = :inputUserId

You should only need the DISTINCT if for some reason you have non-unique rows.


DataTable dtTable = null;
using (SqlConnection oConn = new SqlConnection("Your connection string"))
{
   string strQuery = @"SELECT Distinct userid, username, img1, birthday, gendid, city, state, country, title 
   FROM UserInfo 
   WHERE userinfo.userid IN 
   (
      SELECT MemID 
      FROM network
      WHERE userid= @userid 
    )";
   SqlDataAdapter oDataAdapter = new SqlDataAdapter(strQuery, oConn);
   oDataAdapter.Fill(dtTable, "TableName");

}


@X-Zero's select is good -- one thing is just that the parameter would be @uinputUserID, not :inputUserId, if you're using microsoft (sql-server).

Otherwise, converting this to asp.net (using c#) depends on what control you want to use -- dataset, datatable, datasource, datareader, etc... @Mr.'s data table is a good way, but then you'll need to databind it to whatever control is using it.


Try this (I'm including the using statements in case your not familiar with the rights one to use for ADO.NET):

using System.Data;
using System.Data.SqlClient

public DataTable GetUserDetails(int userID)
{
    DataTable dTable = new DataTable();

    using (SqlConnection con = new SqlConnection(<your connection string>))
    {

        con.Open();

        SqlCommand cmd = new SqlCommand();

        cmd.Parameters.Add(new SqlParameter("@userid", userID);
        cmd.Text = "SELECT DISTINCT u.userid, u.username, u.img1, u.birthday, u.genid, u.city, u.state, u.country, u.title FROM UserInfo u JOIN Network n ON u.userid = n.userid WHERE n.userid = ?";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        SqlDataAdapter da = new SqlDataAdapter(cmd);        

        da.Fill(dTable);
    }

    return dTable;
}


This is not as trivial as the other answers think, because it looks like MemId is a comma-delimited string field (bad database design). Therefore, you have to use dynamic SQL inside... dynamic SQL.

using (SqlConnection conn = new SqlConnection(@"ConnectionStringHere"))
{
    conn.Open();

    IDbCommand cmd = conn.CreateCommand();

    cmd.CommandText = @"
        DECLARE @sql nvarchar(MAX)

        SET @sql = '
            SELECT DISTINCT
                userid, username, img1, birthday,
                            gendid, city, state, country, title 
                FROM UserInfo 
                WHERE UserId IN 
                (' + (SELECT MemID FROM network WHERE UserId = @userId) + ')'

        EXEC(@sql)";

    IDbDataParameter param = cmd.CreateParameter();
    param.DbType = DbType.String;
    param.Value = "12345"; // TODO
    param.ParameterName = "@userId";

    cmd.Parameters.Add(param);

    IDataReader dr = null;

    try
    {
        dr = cmd.ExecuteReader();

        // TODO: Process result set
    }
    finally
    {
        if (dr != null)
            dr.Close();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜