我现在正在通过在课堂上构建登录系统来建立我的网站,
我几乎已经掌握了所有的东西,但它不会返回值。
login.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;
}
}
}
}
但这也是我设置它给集合赋值的方式;得到;
name.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; }
}
}
我的问题在于,它不会给用户带来价值,只有欢迎卡斯珀奥尔森的用户
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;
}
答案 0 :(得分:3)
您的LoginIndhold
方法有两个问题:
LoginInfo
,而您又会返回连接的字符串。readerbruger.Read()
返回false,则您的方法根本不会返回值。你想要做这样的事情:
if (readerbruger.Read())
{
HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
}
return new LoginInfo { Username = username, Password = password };
答案 1 :(得分:2)
此方法:
public LoginInfo LoginIndhold(string username, string password)
表示返回值是LoginInfo类。你正在返回一个字符串。所以你需要在其中创建类存储登录信息并返回它。
尝试:
public LoginInfo LoginIndhold(string username, string password)
{
IndholdName.LoginInfo info;
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());
info.username = username;
info.password = password;
}
return info;
}
答案 2 :(得分:0)
public LoginInfo LoginIndhold(string username, string password)
{
LoginInfo returnValue = null;
using( 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();
using(SqlDataReader readerbruger = cmd.ExecuteReader())
{
if (readerbruger.Read())
{
HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
returnValue = new LoginInfo { Username= username, Password= password};
}
}
}
return returnValue;
}