标签:
说明:之前操作数据库一直都是用拼接字符串,发现很多时候做了很多重复工作,并且还要在每个输入的地方放sql注入,实在是麻烦。
用参数传递,则不用担心sql注入的风险(具体为何,不清楚)。
SqlParameter[] para = new SqlParameter[] { new SqlParameter("@user_name",SqlDbType.NVarChar,10), new SqlParameter("@user_sex",SqlDbType.NVarChar,10), new SqlParameter("@user_age",SqlDbType.NVarChar,10), new SqlParameter("@region",SqlDbType.NVarChar,10) }; para[0].Value = "李立"; para[1].Value = "男"; para[2].Value = "25"; para[3].Value = "常德"; mysql.exeprocedure("test_insert", para); public int exeprocedure(string procename,SqlParameter[] para) { int count; SqlConnection myconn = new SqlConnection(settings); myconn.Open(); SqlCommand mycmd = new SqlCommand(procename,myconn); mycmd.CommandType = CommandType.StoredProcedure; mycmd.CommandText = procename; if (para != null) { mycmd.Parameters.AddRange(para); } count = mycmd.ExecuteNonQuery(); myconn.Close(); return count; }
标签:
原文地址:http://www.cnblogs.com/lilixiang-go/p/5041324.html