string insertstr = "insert into userinfo(name,password,email,phone,mobile,post,address) VALUES('";
insertstr += this._name.Trim() + "','";
insertstr += this._password.Trim() + "','";
insertstr += this._email.Trim() + "','";
insertstr += this._phone.Trim() + "','";
insertstr += this._mobile.Trim() + "','";
insertstr += this._post.Trim() + "','";
insertstr += this._address.Trim() + "')";
Trim() 消耗資源。xp_cmdshell 等危險指令。string strSql = "INSERT INTO SomeTable (c1, c2, c3, ...) VALUES (@c1, @c2, @c3, ...)";
SqlCommand myCommand = new SqlCommand(strSql, myConn);
try {
myCommand.Parameters.Add(new SqlParameter("@c1", SqlDbType.VarChar, 20));
myCommand.Parameters["@c1"].Value = this._Name;
// 依此類推加入所有參數
}
catch { /* 例外處理 */ }
告別字串拼接,參數化查詢是專業開發者的基本功。