gpt4 book ai didi

c# - 它不会返回类中的值

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:02 24 4
gpt4 key购买 nike

我现在正在努力通过在类里面构建它来在我的网站上创建一个登录系统,

我几乎已经准备好所有的东西,但它不会返回值。

登录.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using IndholdName;

namespace LoginClass
{
public class Login
{
public LoginInfo LoginIndhold(string username, string password)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();

cmd.Connection = conn;

cmd.CommandText = "SELECT Id FROM users WHERE Username = @Username AND Password = @Password";
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@Password", password);

conn.Open();
SqlDataReader readerbruger = cmd.ExecuteReader();

if (readerbruger.Read())
{
HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
//It will not return a value
//its error are here,
return username + password;
}
}
}
}

但这也是我设置它以赋予集合值(value)的方式;并得到;

名称.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IndholdName
{
public class LoginInfo
{
public string username { set; get; }
public string password { set; get; }
}
}

我的问题是它不会给用户一个值,这里唯一的欢迎来到 Kasper Olsen

if (readerbruger.Read())
{
HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
//It will not return a value
//its error are here,
return username + password;
}

最佳答案

您的 LoginIndhold 方法有两个问题:

  1. 您声明它应该返回一个 LoginInfo,而您返回的是一个串联的字符串。
  2. 如果 readerbruger.Read() 返回 false,则您的方法根本不会返回值。

你会想做这样的事情:

if (readerbruger.Read())
{
HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
}

return new LoginInfo { Username = username, Password = password };

关于c# - 它不会返回类中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29452925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com