1、SQL 注入
2、使用参数化的方式,可以有效防止SQL注入,使用类parameter的实现类SqlParameter
Command的属性parameters是一个参数集合。
3、举例<查询学生表学生个数>
代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (SqlConnection conn =new SqlConnection("server=.;database=dbtest;uid=sa;pwd=123") ) { string sql = "select Count(*) from userinfo where username='" + textBox1.Text + "' "; SqlCommand cmd = new SqlCommand(sql,conn); conn.Open(); int i = Convert.ToInt32(cmd.ExecuteScalar()); MessageBox.Show(i.ToString()); } } } }