.NET SQLインジェクションの問題

ここでは問題を解決するためのパラメータ化されたSQLインジェクションの方法を使用し、
C#Winフォーム抗SQLインジェクションの言語のユーザーのログインに行うための一例として、

  try
                {
                    string strconn = ConfigurationManager.ConnectionStrings["sqlConn"].ConnectionString;
                    SqlConnection conn = new SqlConnection(strconn);
                    conn.Open();
                    //解决SQL注入问题,使用参数化
                    string sql = "select count(*) from Table_user where id=@Id and pass=@Pass";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                   //给参数复制
                    cmd.Parameters.AddWithValue("@Id", textBox1.Text);
                    cmd.Parameters.AddWithValue("@Pass", textBox2.Text);
                    int res = (int)cmd.ExecuteScalar();
                    if (res !=0)
                    {
                        MessageBox.Show("ok");
                    }
                    else
                    {
                        MessageBox.Show("error");
                    }
                    conn.Close();
                }

       catch (Exception ex)
                {

                  MessageBox.Show("error");
                  MessageBox.Show(ex.Message.ToString() + "失败");
                }

図結果:
中:アカウント「S'または1 = 1 -として、 」 例の
パスワードクッキン。
ここに画像を挿入説明

生の鶏肉料理大学、研究.NET、希望の神のガイダンスがたくさん。

公開された16元の記事 ウォンの賞賛2 ビュー206

おすすめ

転載: blog.csdn.net/weixin_43482965/article/details/104304431