gpt4 book ai didi

c# - 从 SQL Server 数据库中获取字符串值

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:45 27 4
gpt4 key购买 nike

我正在开发一个在 asp.net 中使用并使用 SQL Server 数据库的登录系统。我还没有添加安全部分,这只是用于测试的登录,所以这暂时不是我关心的。

我在数据库中有一个 Account 表,其中包含 usernamepasswordacc_type 列。我在登录期间想要的是,如果凭据正确,则需要检查帐户类型,并且根据帐户类型,用户将被重定向到其相应的页面。

到目前为止,这是我的代码:

protected void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=AA-PC\\SQLSERVER2012;Initial Catalog=oncf;Integrated Security=True";

con.Open();

string query = "SELECT count(*) FROM Account where acc_username=@username and acc_password=@password";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);

int count= Convert.ToInt32(cmd.ExecuteScalar());

if(count==1)
{
string query2 = " SELECT acc_type FROM Account where acc_username=@username and acc_password=@password";

SqlCommand cmd2 = new SqlCommand(query2, con);
cmd2.Parameters.AddWithValue("@username", TextBox1.Text);
cmd2.Parameters.AddWithValue("@password", TextBox2.Text);

DataTable dt = new DataTable();

SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(dt);

string userType = dt.Rows[0]["acc_type"].ToString();

if (userType == "LandAsset")
{
Response.Redirect("ManageLines.aspx");
}
else
{
Response.Redirect("MainAdmin.aspx");
}
}
else {
Label2.Visible = true;
}

con.Close();
}

我有一个外部 if else 条件来检查凭据是否正确,如果是这样,那么我们有逻辑检查它是哪种类型的帐户。

但是,每当我尝试登录时,无论用户是什么类型,我总是被重定向到相同的 MainAdmin.aspx 页面。

有人可以帮忙吗?

最佳答案

你这样做太复杂了!

基本上,如果您的用户和他的密码确实存在于表中,您的第二个查询将返回他的 account_type - 反之亦然,如果该用户不存在,或者如果密码错误,则不会返回任何值(或 NULL)- 只需检查一下。另外:由于您要返回恰好一行,一列,您可以使用更简单的.ExecuteScalar() 而不是填充DataTable然后在其中四处搜索以找到您需要的信息..

所以基本上,这段代码应该做同样的事情:

protected void LoginButton_Click(object sender, EventArgs e)
{
// set up your connection string and query as strings
string connectionString = "Data Source=AA-PC\\SQLSERVER2012;Initial Catalog=oncf;Integrated Security=True";
string query = "SELECT acc_type FROM dbo.Account WHERE acc_username = @username and acc_password = @password;"

// set up your connection and command in "using" blocks
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd2 = new SqlCommand(query, con))
{
// define and set parameters
cmd2.Parameters.Add("@username", SqlDbType.VarChar, 100).Value = TextBox1.Text);
cmd2.Parameters.Add("@password", SqlDbType.VarChar, 100).Value = TextBox2.Text);

// open connection, execute command, close connection
con.Open();

object result = cmd.ExecuteScalar();

con.Close();

// if nothing was returned -> user/password are not valid
if (result == null) {
Label2.Visible = true;
}
else
{
string accountType = result.ToString();

if (accountType == "LandAsset")
{
Response.Redirect("ManageLines.aspx");
}
else
{
Response.Redirect("MainAdmin.aspx");
}
}
}
}

关于c# - 从 SQL Server 数据库中获取字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31616844/

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