//防Sql注入类 SqlChecker
SqlChecker sqlChecker = new SqlChecker();
            //连接数据库字符串
            string UserId = TextBox1.Text;
           // string Password = TextBox2.Text;
             string Password = SqlChecker.EncryptPassword(TextBox2.Text);
           
               string ConStr = System.Configuration.ConfigurationManager.AppSettings["ConnectString"];
               SqlConnection con = new SqlConnection(ConStr);
              con.Open();
            
            //执行Sql,连接数据库
              string strsql = "select distinct UserId,Password,EmpId from LoginTb where UserId='"+UserId+"' and Password='"+Password+"' and State='1' ";
              SqlCommand cmd = new SqlCommand(strsql, con)
              SqlDataReader rs = cmd.ExecuteReader();
            
              if (rs.Read())
              {                  if (UserId != "")
                  {
               
                  Session.Timeout = 120;
                  Session["UserId"] = TextBox1.Text;
                  Session["EmpId"] = rs["EmpId"].ToString();                  Response.Redirect("main1.aspx");
                 
                 }
这么写登不上去C#sql注入

解决方案 »

  1.   

    用带参数的SQL语句。
    cmd.Parameters.Add(....);
      

  2.   

    某些人口中“防注入”是扯淡的,你先问问这些人懂不懂在t-sql语句中字符串常量中的单引号要转移为两个单引号?
      

  3.   

    转移为  ->  转义为不要人云亦云。你先要看通晓了基本的t-sql语法,再学那些时髦的东西。
      

  4.   

    SqlChecker 是什么?你自己定义的?
      

  5.   

    你先了解一下:什么是参数化查询:http://baike.baidu.com/view/3061939.htm参数化查询(Parameterized Query 或 Parameterized Statement)是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数 (Parameter) 来给值,这个方法目前已被视为最有效可预防SQL注入攻击 (SQL Injection) 的攻击手法的防御方式。再者, 这些最基本的 SQL 的增删改查, 找本书或者资料好好练练吧。
      

  6.   

    不要拿变量拼接SQL,那样会直接参与查询,用SqlCommand + SqlParameters
    自己搜一下吧
      

  7.   

    这个我刚学过,
    namespace 数据导入导出
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (odfImport.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                using (FileStream fileStream = File.OpenRead(odfImport.FileName))
                {
                    using (StreamReader streamReader = new StreamReader(fileStream))
                    {
                        using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
                        //创建连接是非常耗时的,因此不要每次操作创建连接
                        {
                            conn.Open();
                            using (SqlCommand cmd = conn.CreateCommand())
                            {
                                cmd.CommandText = "Insert into T_Persons(Name,Age) values(@Name,@Age)";
                                string line = null;
                                while ((line = streamReader.ReadLine()) != null)
                                {
                                    string[] strs = line.Split('#');
                                    string name = strs[0];
                                    int age = Convert.ToInt32(strs[1]);
                                    cmd.Parameters.Clear();//!!!!!把上次执行的所有的参数都把它清除掉
                                //参数不能重复添加,在while中一直用的就是一个SqlCommand对象
                                    cmd.Parameters.Add(new SqlParameter("Name", name));
                                    cmd.Parameters.Add(new SqlParameter("Age", age));
                                    cmd.ExecuteNonQuery();
                                }
                            }
                        }
                    }
                }
                MessageBox.Show("导入成功");
      

  8.   

    SqlChecker 是一个防Sql注入类,他是被引入登陆代码