【问题标题】:Return a value from sql从 sql 返回一个值
【发布时间】:2015-07-13 09:25:26
【问题描述】:

我正在尝试将我的 sql 连接移动到类文件中的方法并返回一个值以检查数据库中是否存在用户名并在页面中显示为文本以向其他人显示该用户名存在。我应该如何修改我的 aspx .cs 页面以在 aspx 代码页中将错误显示为标签?

我在 aspx .cs 页面上的原始代码:

        string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();

            bool exists = false;

            // create a command to check if the username exists
            using (SqlCommand cmd = new SqlCommand("select count(*) from Member where UserName = @UserName", con))
            {
                cmd.Parameters.AddWithValue("UserName", UNameTxtBox.Text);
                exists = (int)cmd.ExecuteScalar() > 0;
            }

            // if exists, show a message error
            if (exists)
                ExistLabel.Text = "UserName exists";

我的类文件代码:

public static string searchusername(string username)
{
    connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connectionString);
    {
        conn.Open();

        bool exists = false;

        using (SqlCommand comm = new SqlCommand("select count(*) from Member where UserName = @UserName", conn))
        {
            comm.Parameters.AddWithValue("@username", username);
            exists = (int)comm.ExecuteScalar() > 0;
        }
        if (exists)
        {
            return "exist";
        }
        return null;
    }

以及我的 aspx cs 文件中的代码:

 MemberDB.searchusername(UNameTxtBox.Text);

【问题讨论】:

  • 错误说明了什么?
  • 没有错误,但是如果用户名存在于数据库中,我如何将存在传递回aspx页面并显示它?
  • 你应该提供你的aspx/设计器页面代码,你想把它放在哪里显示..
  • 我想在设计器页面中显示为标签。

标签: c# asp.net sql-server


【解决方案1】:

在你的页面中添加一个label,并通过该方法将标签的Text属性设置为valuereturned

<asp:Label runat="server" ID="userExists"></asp:Label>

比在后面的代码中这样做

userExists.Text = MemberDB.searchusername(UNameTxtBox.Text);

【讨论】:

  • 小备注:在我看来,在这种情况下最好使用Literral。如果指定了AssociatedControlID 属性,Label 将转换为 HTML &lt;label&gt; 标签,如果未指定该属性,则被(很可能无用)&lt;span&gt; 标签包围。
  • @Bartdude 感谢我同意的评论,但这里的 OP 想要一个标签,所以只需添加代码。
【解决方案2】:
public static bool searchusername(string username)
{
    connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    using(SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open();

        bool exists = false;

        using (SqlCommand comm = new SqlCommand("select count(*) from Member where UserName = @UserName", conn))
        {
            comm.Parameters.AddWithValue("@username", username);
            exists = (int)comm.ExecuteScalar() > 0;
        }
        return exists;
    }

在您的页面中

if(MemberDB.searchusername(UNameTxtBox.Text))
{
    ExistLabel.Text = "UserName exists";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 2018-07-31
    • 2014-10-21
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多