C# 使用asp.net和SQL Server的登录屏幕

C# 使用asp.net和SQL Server的登录屏幕,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我试图创建一个登录页面,但我的登录按钮不起作用。我正在从sql server数据库中选择用户名和密码 int temp = Convert.ToInt32(com.ExecuteScalar().ToString()); 不幸的是,我犯了一个错误 int temp = Convert.ToInt32(com.ExecuteScalar().ToString()); System.Data.SqlClient.SqlException:靠近“”的语法不正确 int temp = Convert

我试图创建一个登录页面,但我的登录按钮不起作用。我正在从sql server数据库中选择用户名和密码

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
不幸的是,我犯了一个错误

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
System.Data.SqlClient.SqlException:靠近“”的语法不正确

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
第27行:

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
代码如下:

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");
}
这行代码:

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
string checkuser = "select * from tb_Login where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' ";
正在向数据库发送查询,并询问:“给我tb_Login中的所有列,其用户名是txtserName框中的值,密码是txtstpassword框中的值。”

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
然后此行将获取第一行第一列的值,并尝试将其转换为整数,如果无法转换,则将失败:

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
将查询更改为仅选择一列:所需的列

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

还要确保您阅读了有关堆栈溢出的,以便查看您的代码如何对您自己的应用程序构成安全威胁。

用户名后缺少一个
=
,应该使用参数化查询。任何人都可以通过SQL注入登录,甚至更糟。此外,密码不应以纯文本形式存储。访问应用程序的通用密码为:
”或1==1--和密码显然是以纯文本存储的。您好,我添加了“=”但我遇到另一个错误“System.NullReferenceException:对象引用未设置为对象的实例。int temp=Convert.ToInt32(com.ExecuteScalar().ToString());”这只是一个让我的登录正常工作的粗略程序。然后我会进入细节谢谢你的帮助。我只是不明白如何更改这个查询:int temp=Convert.ToInt32(com.ExecuteScalar().ToString());不,那条线没问题。您需要更改sql查询,因为它使用的是
select*
,因此它将获得许多列。你只需要得到你需要的专栏。我不知道该列的名称,但如果它是
Column1
,则从tb\u Login中写入
选择Column1,其中Username=
感谢您的帮助。我真的很感谢你和你的时间
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());