SqlDataReader读取方法不起作用

时间:2013-06-24 19:34:46

标签: c# asp.net sqldatareader

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

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>

我哪里出错?

我们将不胜感激。感谢名单

2 个答案:

答案 0 :(得分:0)

它是while(reader.Read())它没有循环。

答案 1 :(得分:0)

试试这个。我在查询中基本上将@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();
    }