2017-04-25 1 views
0

asp.net을 사용하여 데이터베이스에 레코드를 삽입하고 싶지만 실제로 잘 작동하지 않습니다. 내 데이터베이스에서 colum의 데이터 형식은 randomID를 제외하고 모두 varchars입니다. 코멘트는 말한다처럼 당신은 SQL 주입을 방지하기 위해 쿼리의 파라미터를, 또한 경우에 하나의 문자열 사용자가 가지고해야한다,이 내 코드asp.net을 사용하여 데이터베이스에 레코드를 삽입 할 수 없습니다.

public partial class Registratie : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Danesh\Desktop\Workshop\App_Data\Stap1.mdf;Integrated Security=True"); 
    int RandomID = 2; 
    String Notification = "Uw Identificatienummer is: "; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     Random rnd = new Random(); 
     RandomID = rnd.Next(1, 10000000); 
    } 

    protected void BtnStap1_Click(object sender, EventArgs e) 
    {  
     con.Open(); 
     SqlCommand cmd = con.CreateCommand(); 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = " insert into Gebruiker values('" + RandomID + "', '" + Voornaamtxt.Text + "', '" + Tussenvoegseltxt.Text + "', '" + Achternaamtxt.Text + "','" + Emailtxt.Text + "' '" + Niveautxt.Text + "')"; 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     MessageBox.Show(RandomID.ToString(), Notification); 
     Response.Redirect("/Webpages/LoginPage.aspx"); 
    }  
} 
+2

구글 바비 용 테이블. * 읽은 후에는 SQL 문을 생성하는 데 문자열 연결을 사용하면 안되는 이유를 이해할 수 있습니다. 텍스트 상자 중 하나에 리터럴 텍스트' 'Inserted text is here'가 포함되어있어 유효하지 않은 쿼리가 발생했습니다. ''; 사용자로부터 사용자 이름, 암호 선택; - '대신. 이것이 바로 SQL 주입 공격의 작동 방식입니다. 매개 변수화 된 쿼리를 대신 사용하십시오. –

+0

emaitxt.text와 niveautxt.text 사이에 쉼표가 없습니다. –

+0

[SqlConnection] 또는 [IDisposable]을 구현하는 다른 것을 사용하지 마십시오 (https://msdn.microsoft.com/en-us/library/system.idisposable (v = vs.110) .aspx) 필드로. 대신 로컬 변수를 사용하여 using 문으로 래핑하십시오. – mason

답변

-1

삽입 쿼리에 쉼표 (,)를 입력하지 않았습니다.

cmd.CommandText = " insert into Gebruiker values('" + RandomID + "', '" + Voornaamtxt.Text + "', '" + Tussenvoegseltxt.Text + "', '" + Achternaamtxt.Text + "','" + Emailtxt.Text + "'(here) '" + Niveautxt.Text + "')"; 

그래서 이것을 시도

코드,

cmd.CommandText = " insert into Gebruiker values('" + RandomID + "', '" + Voornaamtxt.Text + "', '" + Tussenvoegseltxt.Text + "', '" + Achternaamtxt.Text + "','" + Emailtxt.Text + "','" + Niveautxt.Text + "')"; 
2

입니다

System.Data.SqlClient.SqlException: 'Incorrect syntax near 'Inserted text is here'.'

:하지만 난 여전히이 오류가 특수 문자 (이스케이프 문자 또는 따옴표)가 입력됩니다.

protected void BtnStap1_Click(object sender, EventArgs e) 
{ 
    con.Open(); 
    SqlCommand cmd = con.CreateCommand(); 
    cmd.CommandType = CommandType.Text; 

    var paramsList = new SqlParameter[] 
    { 
     new SqlParameter("@p1", RandomID), 
     new SqlParameter("@p2", Voornaamtxt.Text), 
     new SqlParameter("@p3", Tussenvoegseltxt.Text), 
     new SqlParameter("@p4", Achternaamtxt.Text), 
     new SqlParameter("@p5", string.Join(" ",Emailtxt.Text,Niveautxt.Text), 
    }; 

    cmd.CommandText = "insert into Gebruiker values(@p1, @p2, @p3, @p4, @p5)"; 
    cmd.Parameters.AddRange(paramsList); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    MessageBox.Show(RandomID.ToString(), Notification); 
    Response.Redirect("/Webpages/LoginPage.aspx"); 
} 
관련 문제