簡體   English   中英

使用 asp.net 和 SQL Server 的登錄屏幕

[英]Login screen using asp.net and SQL Server

我正在嘗試創建一個登錄頁面,但我的登錄按鈕不起作用。 我正在從我的 sql server 數據庫中選擇用戶名和密碼。

不幸的是,我收到一個錯誤

System.Data.SqlClient.SqlException: '' 附近的語法不正確

在第 27 行:

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

代碼如下:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
con.Open();

string checkuser = "select * from tb_Login where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' ";

SqlCommand com = new SqlCommand(checkuser, con);

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
con.Close();

if (temp == 1)
{
    con.Open();
    string checkPass = "select Password from tb_Login where Username='" + txtUsername.Text + "'";

    SqlCommand passCom = new SqlCommand(checkPass, con);
    string password = passCom.ExecuteScalar().ToString().Replace(" ", "");

    if (password == txtPassword.Text)
    {
        Session["New"] = txtUsername.Text;
        Response.Write("Correct");
    }
    else
    {
        Response.Write("Not Correct");
    }
}
else
{
    Response.Write("Username not correct");
}

這行代碼:

string checkuser = "select * from tb_Login where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' ";

正在向數據庫發送查詢並詢問:“給我來自 tb_Login 的所有列,其用戶名是 txtUsername 框中的值,密碼在 txtPassword 框中。”

然后這一行將獲取第一行第一列的值並嘗試將其轉換為整數,如果不能,它將失敗:

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

更改您的查詢以僅選擇一列:您需要的列。

還要確保您在 Stack Overflow 上閱讀了這個問題,以便了解您的代碼如何對您自己的應用程序構成安全威脅。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM