Guest User

Untitled

a guest
Jun 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. String sql =
  2. "SELECT [UserId] FROM [UserProfiles] WHERE NOT [UserId] = 'CurrentUserId'";
  3.  
  4. string strCon = System.Web
  5. .Configuration
  6. .WebConfigurationManager
  7. .ConnectionStrings["SocialSiteConnectionString"]
  8. .ConnectionString;
  9.  
  10. SqlConnection conn = new SqlConnection(strCon);
  11. SqlCommand comm = new SqlCommand(sql, conn);
  12. conn.Open();
  13.  
  14. /*
  15. This is where I need to know how to retrieve the information from the
  16. above command(comm). I am looking for something similiar to php's
  17. mysql_result. I want to access the records kind of like an array or some
  18. other form of retrieving all the data.
  19. Also when the new SqlCommand is called...does that actual run the
  20. SELECT STATEMENT or is there another step.
  21. */
  22.  
  23. conn.Close();
  24.  
  25. String sql = "SELECT [UserId] FROM [UserProfiles] WHERE NOT [UserId] = 'CurrentUserId'";
  26.  
  27. string strCon = System.Web
  28. .Configuration
  29. .WebConfigurationManager
  30. .ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
  31.  
  32. SqlConnection conn = new SqlConnection(strCon);
  33. SqlCommand comm = new SqlCommand(sql, conn);
  34. conn.Open();
  35. SqlDataReader nwReader = comm.ExecuteReader();
  36. while (nwReader.Read())
  37. {
  38. int UserID = (int)nwReader["UserID"];
  39. // Do something with UserID here...
  40. }
  41. nwReader.Close();
  42. conn.Close();
  43.  
  44. using System;
  45. using System.Configuration;
  46.  
  47. public partial class Global : HttpApplication
  48. {
  49. public static string ConnectionString;
  50.  
  51. void Application_Start(object sender, EventArgs e)
  52. {
  53. ConnectionString = ConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
  54. }
  55. ...
  56. }
  57.  
  58. SqlConnection conn = new SqlConnection(Global.ConnectionString);
  59.  
  60. using (BSDIQuery qry = new BSDIQuery())
  61. {
  62. SqlDataReader nwReader = qry.Command("SELECT...").ReturnReader();
  63. // If I needed to add a parameter I'd add it above as well: .ParamVal("CurrentUser")
  64. while (nwReader.Read())
  65. {
  66. int UserID = (int)nwReader["UserID"];
  67. // Do something with UserID here...
  68. }
  69. nwReader.Close();
  70. }
  71.  
  72. public DataTable ExecuteQueryTable(string query)
  73. {
  74. return ExecuteQueryTable(query, null);
  75. }
  76.  
  77. public DataTable ExecuteQueryTable(string query, Dictionary<string, object> parameters)
  78. {
  79. using (SqlConnection conn = new SqlConnection(this.connectionString))
  80. {
  81. conn.Open();
  82.  
  83. using (SqlCommand cmd = conn.CreateCommand())
  84. {
  85. cmd.CommandText = query;
  86.  
  87. if (parameters != null)
  88. {
  89. foreach (string parameter in parameters.Keys)
  90. {
  91. cmd.Parameters.AddWithValue(parameter, parameters[parameter]);
  92. }
  93. }
  94.  
  95. DataTable tbl = new DataTable();
  96. using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  97. {
  98. da.Fill(tbl);
  99. }
  100.  
  101. return tbl;
  102. }
  103. }
  104. }
  105.  
  106. String sql = "SELECT [UserId] FROM [UserProfiles] WHERE [UserId] != @CurrentUserId";
  107.  
  108. string strCon = System.Web
  109. .Configuration
  110. .WebConfigurationManager
  111. .ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
  112.  
  113. DataTable result = new DataTable();
  114. using (var conn = new SqlConnection(strCon))
  115. using (var cmd = new SqlCommand(sql, conn))
  116. {
  117. cmd.Parameters.Add("@CurrentUserID", SqlDbType.Int).Value = CurrentUserID;
  118. conn.Open();
  119.  
  120. result.Load(cmd.ExecuteReader());
  121. }
  122.  
  123. while ($row = mysql_fetch_array ($result))
  124. {
  125. //this assumes you're doing something with foo in loop
  126. $foo = $row["userid"];
  127.  
  128. //using $foo somehow
  129. }
  130.  
  131. //assuming you have a DataSet called myDataSet
  132. for (int i = 0; i < myDataSet.Tables[0].Rows.Count; i++)
  133. {
  134. //likewise assuming here you're doing something with foo in loop
  135. string foo = myDataSet.Tables[0].Rows[i]["userid"].ToString();
  136.  
  137. //similarly do something with foo in loop
  138. }
Add Comment
Please, Sign In to add comment