Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将两个SQL查询合并为一个查询并转换为C_C#_Asp.net_Sql_Vbscript_Asp Classic - Fatal编程技术网

C# 将两个SQL查询合并为一个查询并转换为C

C# 将两个SQL查询合并为一个查询并转换为C,c#,asp.net,sql,vbscript,asp-classic,C#,Asp.net,Sql,Vbscript,Asp Classic,我需要将这两个单独的查询合并为一个查询,因为它效率不高,需要将它从经典的ASP.NET转换为C ASP.NET 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 = "S

我需要将这两个单独的查询合并为一个查询,因为它效率不高,需要将它从经典的ASP.NET转换为C ASP.NET

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 UserInfo 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

我不知道C,但是SQL是一个经典的连接

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

如果由于某些原因您有非唯一的行,您应该只需要DISTINCT。

@X-Zero的选择很好-一件事是,如果您使用的是microsoft sql server,那么参数将是@uinputUserID,而不是:inputUserId

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");

}

否则,使用c将其转换为asp.net取决于您想要使用的控件-dataset、datatable、datasource、datareader等@Mr.的数据表是一种很好的方法,但是您需要将其数据绑定到使用它的任何控件。

尝试此方法,我将包括using语句,以防您不熟悉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;
}

这并不像其他答案所想的那么简单,因为MemId看起来像是一个逗号分隔的字符串字段,这是一个糟糕的数据库设计。因此,您必须在内部使用动态SQL。。。动态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();
    }
}

很好的例子-我看到的一个小问题是@userid-它没有设置在代码中的任何地方。我认为最好像我的示例中那样进行参数化查询。否则,你就把我揍得体无完肤了——我打字慢了一天。