我是新來的asp.net,我正在寫一個登錄腳本&學習數據庫應用程序。但腳本似乎不起作用。它仍然可以添加重複的用戶名。這裏是腳本Asp.net登錄腳本
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class Registration : System.Web.UI.Page
{
static string temp;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["earchConnectionString"].ConnectionString);
con.Open();
string cmdStr = "Select count(*) from [user] where UserName='" + TextBoxUN.Text + "'";
SqlCommand userExist = new SqlCommand(cmdStr, con);
int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
con.Close();
if (temp == 1)
{
Response.Write("User Name Already Exist....<br /> Please Choose Another User Name.");
}
}
}
protected void Submit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["earchConnectionString"].ConnectionString);
con.Open();
string insCmd = "Insert into [user] (UserName, Password, EmailAddress, FullName, level) values (@UserName,@Password,@EmailAddress, @FullName, @level)";
SqlCommand insertUser = new SqlCommand(insCmd, con);
insertUser.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
insertUser.Parameters.AddWithValue("@Password", TextBoxPass.Text);
insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
insertUser.Parameters.AddWithValue("@FullName", TextBoxFN.Text);
insertUser.Parameters.AddWithValue("@level", level.SelectedValue.ToString());
try
{
insertUser.ExecuteNonQuery();
con.Close();
//Response.Redirect("Login.aspx");
Label1.Text = temp;
}
catch (Exception er)
{
Response.Write("Something wrong");
}
finally
{
//Any Special Action You Want To Add
}
}
}
任何可以檢測到的問題?
感謝
你真的應該參數化你的第一個查詢 - 你直接將用戶輸入提供給一個sql查詢 - http://en.wikipedia.org/wiki/SQL_injection – dash
是的,肯定參數化該查詢,否則你的應用程序可以使用[ SQL注入](http://en.wikipedia.org/wiki/SQL_injection)。請參閱[這裏](http://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CCoQFjAB&url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us %2Fmagazine%2Fcc163917.aspx&ei = 8TV1ULm3AqLG0QWKsoGIDw&usg = AFQjCNE8G1Y5 - o-pR -19VgTgXKckluPkw&sig2 = kiYfpNLn8C0nt25PntB88w)獲得更多幫助。 –