SqlDataReader Read方法不工作

本文关键字:工作 方法 Read SqlDataReader | 更新日期: 2023-09-27 18:19:40

我正在尝试从数据库中检索列值,这是我的代码

protected void LoadProfile()
{
        conn = new SqlConnection(connString);
        cmdProfile = new SqlCommand("SELECT Name, Father, Gender, UserName, City, Country, Department, Year, Degree, JobTittle, Organization, JobCity, JobCountry, JobTittle FROM UserProfile WHERE UserName=@UserName", conn);
        cmdProfile.Parameters.AddWithValue("@UserName", userName);
        conn.Open();
        reader = cmdProfile.ExecuteReader();
        if (reader.Read())
        {
            labelName.Text = reader["Name"].ToString();
            txtBoxFather.Text = reader["Father"].ToString();
            TextBoxGender.Text = reader["Gender"].ToString();
            TextBoxAge.Text = "";
            TextBoxCity.Text = reader["City"].ToString();
            TextBoxCountry.Text = reader["Country"].ToString();
            TextBoxDepartment.Text = reader["Department"].ToString();
            TextBoxDegree.Text = reader["Degree"].ToString();
            TextBoxYear.Text = reader["Year"].ToString();
            TextBoxJobTittle.Text = reader["JobTittle"].ToString();
            TextBoxJobCity.Text = reader["JobCity"].ToString();
            TextBoxJobCountry.Text = reader["JobCountry"].ToString();
            TextBoxOrganization.Text = reader["Organization"].ToString();
        }
        conn.Close();
    } 

但它并没有从数据库中检索任何内容。

实际上,我使用这行将userName参数作为来自不同页面的查询字符串

 userName = Request.QueryString["Name"].ToString();

当我设置断点时,控制不会在这条线之后继续

if (reader.Read())

这是我获取查询字符串的页面。

<asp:GridView ID="GridAllAlumni" runat="server" 
        onitemcommand="GridAllAlumni_ItemCommand">
   <Columns>
      <asp:TemplateField>
         <ItemTemplate>
            <asp:LinkButton ID="lnkname" runat="server"
                            Text='<%#Eval("Name") %>'
                            PostBackUrl='<%#"~/Profile/Profile.aspx?Name="+Eval("Name") %>'/>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>

我哪里错了?

我们将感谢您的帮助。Thanx

SqlDataReader Read方法不工作

它是while(reader.Read())它不是循环的。

试试这个。我在查询中基本上将@UserName更改为:UserName,并删除了cmdProfile.Parameters.AddWithValue("@UserName", userName);中的@。它对我有效。但是,我可能错了。所以,请随时纠正我。

protected void LoadProfile()
{
        conn = new SqlConnection(connString);
        cmdProfile = new SqlCommand("SELECT Name, Father, Gender, UserName, City, Country, Department, Year, Degree, JobTittle, Organization, JobCity, JobCountry, JobTittle FROM UserProfile WHERE UserName=:UserName", conn);
        cmdProfile.Parameters.AddWithValue("UserName", userName);
        conn.Open();
        reader = cmdProfile.ExecuteReader();
        if (reader.Read())
        {
            labelName.Text = reader["Name"].ToString();
            txtBoxFather.Text = reader["Father"].ToString();
            TextBoxGender.Text = reader["Gender"].ToString();
            TextBoxAge.Text = "";
            TextBoxCity.Text = reader["City"].ToString();
            TextBoxCountry.Text = reader["Country"].ToString();
            TextBoxDepartment.Text = reader["Department"].ToString();
            TextBoxDegree.Text = reader["Degree"].ToString();
            TextBoxYear.Text = reader["Year"].ToString();
            TextBoxJobTittle.Text = reader["JobTittle"].ToString();
            TextBoxJobCity.Text = reader["JobCity"].ToString();
            TextBoxJobCountry.Text = reader["JobCountry"].ToString();
            TextBoxOrganization.Text = reader["Organization"].ToString();
        }
        conn.Close();
    }