2011-07-14 77 views
1

我需要這兩個單獨的查詢,因爲它不是有效的結合成一體,並具有這種轉換爲C#ASP.NET從經典ASP結合2個SQL查詢到一個查詢,並轉換爲C#

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 

回答

0

我不知道有關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如果由於某種原因,你有非唯一的行。

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

} 
+0

很好的例子 - 我看到的一個小問題是@userid - 它沒有在代碼中的任何位置設置。我認爲在我的例子中做參數化查詢會更好。否則,你打敗了我(打字時間慢)。 – Tim

0

@ X-Zero的選擇是不錯的 - 一件事就是,參數會被@uinputUserID,不:inputUserId,如果你使用的是Microsoft(SQL服務器)。

否則,將其轉換爲asp.net(使用c#)取決於您要使用的控件 - 數據集,數據表,數據源,數據讀取器等... @先生的數據表是一種好方法,但是你需要將它綁定到任何使用它的控件上。

0

試試這個(我包括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; 
} 
0

爲其他答案覺得這不是微不足道的,因爲它看起來像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(); 
    } 
}