我正在尝试进行这么多会话,以便检索在我的网站中为其创建会话的个人的信息。现在,执行标量只为我分配给此处创建的不同会话的所有标签提供了一个值。我很天真,并试图学习这个,但我无法理解执行阅读器命令...所以请尽可能编写代码..
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Select * from Student where StudId=@sid and Password=@pw", con);
cmd.Parameters.AddWithValue("@sid", TextBox1.Text);
cmd.Parameters.AddWithValue("@pw", TextBox2.Text);
con.Open();
string name = Convert.ToString(cmd.ExecuteScalar());
string lastname = Convert.ToString(cmd.ExecuteScalar());
string gender = Convert.ToString(cmd.ExecuteScalar());
string dob = Convert.ToString(cmd.ExecuteScalar());
string pno = Convert.ToString(cmd.ExecuteScalar());
string address = Convert.ToString(cmd.ExecuteScalar());
string branch = Convert.ToString(cmd.ExecuteScalar());
string library = Convert.ToString(cmd.ExecuteScalar());
string bus = Convert.ToString(cmd.ExecuteScalar());
string hostel = Convert.ToString(cmd.ExecuteScalar());
string semester = Convert.ToString(cmd.ExecuteScalar());
string fname = Convert.ToString(cmd.ExecuteScalar());
string mname = Convert.ToString(cmd.ExecuteScalar());
string fpno = Convert.ToString(cmd.ExecuteScalar());
string mpno = Convert.ToString(cmd.ExecuteScalar());
string email = Convert.ToString(cmd.ExecuteScalar());
string img = Convert.ToString(cmd.ExecuteScalar());
con.Close();
Session.Add("StudId", TextBox1.Text);
Session.Add("StudFirstName", name);
Session.Add("StudLastName", lastname);
Session.Add("Gender", gender);
Session.Add("DateOfBirth", dob);
Session.Add("PhoneNo", pno);
Session.Add("Address", address);
Session.Add("BranchId", branch);
Session.Add("Library", library);
Session.Add("Bus", bus);
Session.Add("Hostel", hostel);
Session.Add("SemesterId", semester);
Session.Add("FatherName", fname);
Session.Add("MotherName", mname);
Session.Add("FatherPhoneNo", fpno);
Session.Add("MotherPhoneNo", mpno);
Session.Add("EmailID", email);
Session.Add("StudImg", img);
FormsAuthentication.RedirectFromLoginPage(name, false);
答案 0 :(得分:1)
sqldatareader的一个例子是这样的:
SqlConnection obj_con;
SqlCommand obj_com;
try
{
obj_con = new SqlConnection("your connection string");
obj_com = new SqlCommand();
obj_com.Connection = obj_con;
obj_com.CommandText = ("Select * from Student where StudId=@sid and Password=@pw";)
SqlParameter p_Id = new SqlParameter("@sid", SqlDbType.NVarChar);
p_Id.Direction = ParameterDirection.Input;
p_Id.Value = sid;
obj_com.Parameters.Add(p_Id);
SqlParameter p_pass = new SqlParameter("@pw", SqlDbType.NVarChar);
p_pass.Direction = ParameterDirection.Input;
p_pass.Value = pw;
obj_com.Parameters.Add(p_pass);
obj_con.Open();
SqlDataReader obj_rd = obj_com.ExecuteReader();
while (obj_rd.Read())
{
StudFirstName = obj_rd[0].ToString();
StudLastName = obj_rd[1].ToString();
StudId = obj_rd[2].ToString();
Gender = obj_rd[3].ToString();
Address = obj_rd[4].ToString();
EmailId = obj_rd[5].ToString();
BranchId = obj_rd[6].ToString();
SemesterId = obj_rd[7].ToString();
}
obj_con.Close();
}
catch (Exception s)
{
}
如果您有多个结果,则可以创建网址列表列表网址,并将每个结果添加到您的列表中。 obj_rd [0] 指的是第一列在我的结果中。所以你有更多的那一列,这样你就可以得到另一列的结果: obj_rd [1],obj_rd [...]