好的网站都是用存储过程的吗,而不是在代码里用SQL语句

来源:百度知道 编辑:UC知道 时间:2024/06/12 21:41:05
可是我觉得用存储过程的加 代码要多很多
-------------------------------------------------------
我看我的代码,比原来多多了(我加分了,帮忙解释一下)
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "proc_selectjiating";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter pa = new SqlParameter("@jiatingname", SqlDbType.VarChar);
SqlParameter pa1 = new SqlParameter("@mima",SqlDbType.VarChar);
pa.Direction = ParameterDirection.Input;
pa1.Direction = ParameterDirection.Input;
pa.Value = TextBox1.Text;
pa1.Value = TextBox1.Text;
da.SelectCommand.Parameters.Add(pa);
da.SelectCommand.Parameters.Add(pa1);
本来是由三四句代码的
-----------------
其实我的意思不是让你解释这段代码,我是想说用了存储过程,代码

(1) 存储过程通过参数传递,安全性高,可防止注入式攻击.

(2) 查询的语句在存储过程里,与程序不相关,如果以后要修改程序或者数据库,都不会出现连锁反应,增加系统可扩展性.

(3) 网站执行查询的时候,只需要传递简单的参数就可以了,无论是代码优化上还是查询优化上都可以做到高效.

(4) 允许模块化编程,即,可以将一组查询写在一个过程里面,然后在程序里直接调用,而不必每次都写若干个语句来实现相应功能

===============================================
??

有些不明白......

怎么会加很多?

只需要改个名字就行了呀....

Example: =========这里是原来的代码

/// <summary>
/// 检查用户帐号状态
/// </summary>
/// <param name="logName">登录名称</param>
/// <returns>如果帐号已激活,返回true</returns>
private bool CheckAccouontState(string logName)
{
string sql = "select count(*) from TSR_PASSPORT where USR_NAME_MAIN=@loginName and USR_STATE='已激活'";
int num = 0;

SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(