问题描述
当我进入这个PROGRAMM用户名和密码进入数据库,并从表比较,但是当我输入用户名admin,密码admin(在表中)
compalier显示误差本着附近管理不正确的语法
INT TEMP = Convert.ToInt32(com.ExecuteScalar()的ToString());
保护无效的button1_Click(对象发件人,EventArgs的发送)
{ 康涅狄格州的SqlConnection =新的SqlConnection(@数据源= \\ SQLEX $ P $干燥综合征; AttachDbFilename = C:\\用户\\ 1 \\文件\\ DB.mdf;集成安全性= TRUE;连接超时= 30;用户实例=真) ;
conn.Open();
从[用户]其中用户名SELECT COUNT(*)'+ TextBoxUserName.Text +'的字符串checkuser =;
COM的SqlCommand =新的SqlCommand(checkuser,康涅狄格州);
INT TEMP = Convert.ToInt32(com.ExecuteScalar()的ToString());
conn.Close(); 如果(临时== 1)
{
conn.Open();
字符串checkpassword =从用户那里密码密码'+ TextBoxPassword.Text +';
的SqlCommand passComm =新的SqlCommand(checkpassword,康涅狄格州);
。字符串密码= passComm.ExecuteScalar()的ToString();
如果(密码== TextBoxPassword.Text)
{
//会话[NEW] = TextBoxUserName.Text;
的Response.Redirect(Welcome.aspx);
} 其他
{
的Response.Redirect(Error.aspx);
} }
时,只需通过缺失导致错误的SQL命令文本串联值之前等于。
但也修复它,你的code是错误的其他原因。
- 您应该始终使用参数化查询,以避免Sql注射和解析的问题,
- 您可以删除导致的所有记录不必要的负担只是为了确认您所查找数据的存在COUNT函数
- 您需要确定您的用户搜索密码和
用户名在相同的记录,因为它是现在,在code上面的搜索第一的用户名
然后输入密码,但我可以键入现有的用户名(如果第一次通过),并使用
不同的用户的密码(如果第二次传递),然后获得
您的网站。
字符串checkuser =IF EXISTS(选择1从[用户]其中用户名= @usr和密码= @密码)
选择1,否则选择0;
使用(SqlConnection的康恩=新的SqlConnection(....))
使用(COM的SqlCommand =新的SqlCommand(checkuser,康涅狄格州))
{
conn.Open();
com.Parameters.AddWithValue(@ USR,TextBoxUserName.Text);
com.Parameters.AddWithValue(@密码,TextBoxPassword.Text);
INT TEMP = Convert.ToInt32(com.ExecuteScalar());
如果(临时== 1)
的Response.Redirect(Welcome.aspx);
其他
的Response.Redirect(Error.aspx);
}
在这个例子改变以上是using语句,以确保您的连接和命令设置在操作结束也异常的情况下,其他的事情
this programm when i enter username and password go to data base and compare from table,but when i enter username admin ,password admin(exist in table)compalier show error "Incorrect syntax near 'admin'" in lineint temp = Convert.ToInt32(com.ExecuteScalar().ToString());
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\1\Documents\DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
conn.Open();
string checkuser = "select count(*) from [Users] where Username '" + TextBoxUserName.Text + "'";
SqlCommand com = new SqlCommand(checkuser,conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
conn.Close();
if (temp == 1)
{
conn.Open();
string checkpassword = "select Password from Users where Password'" + TextBoxPassword.Text + "'";
SqlCommand passComm = new SqlCommand(checkpassword, conn);
string password = passComm.ExecuteScalar().ToString();
if (password == TextBoxPassword.Text)
{
//Session["NEW"] = TextBoxUserName.Text;
Response.Redirect("Welcome.aspx");
}
else
{
Response.Redirect("Error.aspx");
}
}
The error is simply caused by the missing equals before the values concatenated in the sql command text.
But also fixing it, your code is wrong for other reasons.
- You should ALWAYS use a parameterized query to avoid Sql Injection and parsing problems,
- You could remove the COUNT function that causes an unnecessary load of all records just to confirm the existence of your searched data
- You need to identify your user searching for both password andusername on the SAME record, as it is now, the code above search first the usernameand then a password, but I can type an existing user name (first if passed) and usea password of a different user (second if passed) and then gain access toyour site.
.
string checkuser = "IF EXISTS(select 1 from [Users] where Username = @usr AND Password=@pwd)
SELECT 1 ELSE SELECT 0";
using(SqlConnection conn = new SqlConnection(....))
using(SqlCommand com = new SqlCommand(checkuser,conn))
{
conn.Open();
com.Parameters.AddWithValue("@usr", TextBoxUserName.Text);
com.Parameters.AddWithValue("@pwd", TextBoxPassword.Text);
int temp = Convert.ToInt32(com.ExecuteScalar());
if (temp == 1)
Response.Redirect("Welcome.aspx");
else
Response.Redirect("Error.aspx");
}
Other things changed in the example above are the USING STATEMENT to be sure that your connection and command are disposed at the end of the operation also in case of exceptions
这篇关于"附近有语法错误“管理员”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!