2017-12-30 12 views
-4

このコードを実行するとエラーが発生し、このエラーを解決するのに役立ちます。 '='付近の構文が正しくありません。'='付近の構文が正しくありません

これはどんな種類のエラーですか?

namespace SqlCommandBuilders 
{ 
    public partial class WebForm1: System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      String CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; 
      SqlConnection con = new SqlConnection(CS); 
      string sqlQuery = "Select * from tblStudents where ID = "+txtStudentID.Text; 
      SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "Students"); 

      ViewState["SQL_QUERY"] = sqlQuery; 
      ViewState["DATASET"] = ds; 

      if(ds.Tables["Students"].Rows.Count > 0) 
      { 
       DataRow dr = ds.Tables["Students"].Rows[0]; 
       txtStudentID.Text = dr["Name"].ToString(); 
       txtTotalMarks.Text = dr["TotalMarks"].ToString(); 
       ddlGender.SelectedValue = dr["Gender"].ToString(); 
      } 
      else 
      { 
       lblStatus.ForeColor= System.Drawing.Color.Red; 
       lblStatus.Text = "No Student Record with ID =" + txtStudentID.Text; 
      } 

     } 
    } 
} 
+0

エラーが発生した行を表示してください。完全なエラー/例外の詳細を投稿してください。 –

答えて

-3

Sql注入につながる可能性がありますので、実際には実行しないでください。以下のクエリは、エラーを削除する必要があります。

これにSQL文を変更します。

string sqlQuery = "Select * from tblStudents where ID = '"+txtStudentID.Text"'"; 

理想の構文は次のようになります。

sqlCommand.CommandText = "Select * from tblStudents where ID = @text;"; 
sqlCommand.Parameters.AddWithValue("@text",txtStudentID.Text); 
+2

[**いいえ、決して行ってはいけません。**](http://bobby-tables.com/) –

+1

ダウンローダは、自分の行動を説明するよう要求されます。 –

+0

@ T.J.Crowderここではエラーを解決しています。 SQLインジェクションの防止を提案することは、この質問の範囲ではありません。質問者はSQLインジェクションを避けるためにSqlParametersを使用することがあります。 –

1

SQLは、ユーザーが使用するテキスト入力は、ほとんどの場合、SQLインジェクション攻撃と構文エラーを回避するために、パラメータ化クエリを使用する必要があり、それが取得することも良いことだというコマンド

DataSet ds = new DataSet(); 
using(SqlConnection con = new SqlConnection(CS)) { 
    string sqlQuery = "Select * from tblStudents where ID = @studentId"; 
    using(SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con)) { 
     da.SelectCommand.Parameters.Add("@studentId", SqlDbType.VarChar) 
            .Value = txtStudentID.Text; 
     da.Fill(ds, "Students"); 
    } 
} 
3

ティ:using文で(データベース接続など)使い捨てのオブジェクトをラップするのが習慣であなたが今作成している文字列についてのnk。 txtStudentID.Textが文字列Joeであるとします。あなたは明らかに間違っているSelect * from tblStudents where ID = Joeを作成しています。 Joeには引用符が必要です。

ただし、の場合は、引用符を付けるだけではありません。 Here's why

enter image description here

here上記のリンク先サイトに記載されているように、パラメータ化されたステートメントを使用している行うには正しいこと。あなたのコードにその例を適用すると、我々のようなものを取得したい:

SqlCommand sqlQuery = new SqlCommand("Select * from tblStudents where ID = @username", con); 
sqlQuery.Parameters.AddWithValue("@username", txtStudentID.Text); 

を...しかし、私はあなたのViewState事があるかわからないので、あなたがそれを適用助けることはできません。

0

ここにカップルのものがあります。

このような場合は、SQLパラメータを常に使用する必要があります。

また、学生IDはデータベースのテキストフィールドまたは数字ですか?

数字の場合、テキストボックスはどこに初期化されていますか? page_loadは最初に起こるものの1つで、これはすべてのpage_load(これも最初のもの)で実行しているので、空の文字列の場合はパラメータを使用するかどうかにかかわらずクラッシュします文字列を数値に変換することはできません。

関連する問題