[英]Login form with SQL Server database in asp.net (C#)
我正在尝试在ASP.net中创建一个登录系统。 它没有显示任何错误,但仍然无法正常工作。
这是代码:
SqlConnection connectionstring = new SqlConnection("Server=.\\SQLEXPRESS;Database=TestDB;User Id=AMS; Password = password; ");
public void LoadData() //<-- call this when the button is pressed
{
try
{
SqlCommand SelectCommand = new SqlCommand("SELECT `Username`, `Password` FROM `LoginData` WHERE `Username` = '" + TextBox1.Text + "' AND `Password` = '" + TextBox2.Text + "'", connectionstring);
SqlDataReader myReader;
connectionstring.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
string script = "alert(\"Login successful!\");";
ScriptManager.RegisterStartupScript(this, GetType(),"ServerControlScript", script, true);
connectionstring.Close();
}
else if (count == 0)
{
string script = "alert(\"Login Failed!\");";
ScriptManager.RegisterStartupScript(this, GetType(),"ServerControlScript", script, true);
connectionstring.Close();
}
else
{
}
}
catch (Exception ex)
{
connectionstring.Close();
}
}
这根本不起作用。 而且我不知道为什么。
这是CSS中按钮的代码
#Button1{
background-color: #0066CC;
font-family: 'Segoe UI',Tahoma,Arial,Helvetica,sans-serif;
margin-right: 10px;
cursor: pointer;
color: #fff;
padding: 10px 15px;
font-size: 14px;
display: inline-block;
margin-bottom: 18px;
z-index: 1;
position: absolute;
top: 203px;
left: 383px;
width: 130px;
}
这是在asp.net
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
它没有显示任何错误,也没有任何显示,只是在单击按钮时刷新页面。 问题是什么? 我找不到。 也许是在SQL连接字符串?
编辑:史蒂夫建议删除尝试/抓住
这是我得到的错误
System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理
附加信息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 验证实例名称正确和SQL
修复了由Sql Server Express的无效配置(可能未启用所需的协议(TCP))导致的错误后,您还应注意,Sql Server不在字段名和表名周围使用反引号。字段名称和保留字之间的区别Sql Server使用方括号。
说您的代码应重写为使用参数,而不是字符串连接。 众所周知,字符串连接是构建查询的错误方法,因为它很容易引入错误并允许Sql Injection攻击
public void LoadData()
{
string cmdText = @"SELECT 1 FROM LoginData
WHERE Username = @name AND AND Password = @pass";
using(SqlConnection cnn = new SqlConnection("Server=.\\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;"))
using(SqlCommand SelectCommand = new SqlCommand(cmdText, cnn))
{
SelectCommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = TextBox1.Text;
SelectCommand.Parameters.Add("@pass", SqlDbType.NVarChar).Value = TextBox2.Text;
cnn.Open();
using(myReader = SelectCommand.ExecuteReader())
{
// No need to read anything. If your reader has rows then
// the user and its password exists
// (you don't allow two users have the same name right?)
if(myReader.HasRows)
{
string script = "alert(\"Login successful!\");";
ScriptManager.RegisterStartupScript(this, GetType(),"ServerControlScript", script, true);
}
else
{
string script = "alert(\"Login Failed!\");";
ScriptManager.RegisterStartupScript(this, GetType(),"ServerControlScript", script, true);
}
}
}
}
注意,我在一次性对象周围使用了using语句,以确保在发生异常的情况下也关闭并丢弃这些对象。
另一件事是使用TrustedConnection = True对数据库服务器使用Windows身份验证,并且不需要特定用户登录。当然,如果您需要将程序部署到客户PC并取决于客户,这将会改变。要求。
我认为您的问题是OnClick="Button1_Click"
。 您确定已调用LoadData
方法(可能是在Button1_Click
事件中)?
SqlConnection
可能引发异常。 在您的catch
添加一条消息:
catch (Exception ex)
{
string script = String.Format("alert('Error occured creating user. Exception message: {0}');", ex.Message);
ScriptManager.RegisterStartupScript(this, GetType(),"ServerControlScript", script, true);
connectionstring.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.