string sSQL = "INSERT INTO TB_USER(UID,Account,Password,Gender,Phone,Mobile,Email,IsEnabled,Comment) VALUES('{0}','{1}','{2}',{3},'{4}','{5}','{6}',{7},'{8}')";
sSQL = string.Format(sSQL, User.UID, User.Account, User.Password, User.Gender, User.Phone, User.Mobile, User.Email, User.IsEnabled, User.Comment);
有如上一段程序,主要是用来拼接SQL字符串,但是如果字符串参数含有单引号会存在问题,所以我想重载string.Format函数,判断如果参数是字符串,替换里面的单引号为两个单引号有什么办法?
自己另外写个方法不好吗?
static void CreateSqlParameterOutput()
{
SqlParameter parameter = new SqlParameter("Description", SqlDbType.VarChar, 88);
parameter.Direction = ParameterDirection.Output;
}
cmd.CommandText = sql;
cmd.Parameters.Add("@id", 100);
cmd.CommandType = CommandType.Text;
object id = cmd.ExecuteScale();
使用这种形式不会影响效率,只会提高精度度,也不能算麻烦。因为对一个参数来说,应该给它指定名称,类型,大小传入传出等参数。如果不指定,系统会自己花时间来匹配。