广交好友~~ 想要讨论的可以留下msn~~~ 希望群友网友经常能提出问题,一起解决,共同提高

SQL 问答2 存储过程的作用

上一篇 / 下一篇  2009-12-10 23:27:44 / 个人分类:SQL

(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("@loginName", SqlDbType.VarChar, 100, "USR_NAME_MAIN").Value = logName;
cmd.Connection = con;
cmd.CommandText = sql;
try
{
con.Open();
num = Convert.ToInt16(cmd.ExecuteScalar());
}
catch (SqlException ex)
{
lblMsg.Text = ex.Message + ",错误编号:(6001)";
}
finally
{
con.Close();
con.Dispose();
cmd.Dispose();
}

if (num == 0)
{
return true;
}
else
{
return false;
}
}

=================== 修改成这样:

/// <summary>
/// 检查用户帐号状态
/// </summary>
/// <param name="logName">登录名称</param>
/// <returns>如果帐号已激活,返回true</returns>
private bool CheckAccouontState(string logName)
{
/************ SQL 语句就不用了....
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("@loginName", SqlDbType.VarChar, 100, "USR_NAME_MAIN").Value = logName;
cmd.Connection = con;
// cmd.CommandText = sql;
cmd.CommandText = "存储过程名";
cmd.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
num = Convert.ToInt16(cmd.ExecuteScalar());
}
catch (SqlException ex)
{
lblMsg.Text = ex.Message + ",错误编号:(6001)";
}
finally
{
con.Close();
con.Dispose();
cmd.Dispose();
}

if (num == 0)
{
return true;
}
else
{
return false;
}
}
=======================================================
-------------------------------------------------------
我看我的代码,比原来多多了(我加分了,帮忙解释一下)
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "proc_selectjiating"; // 这里是存储过程名称
da.SelectCommand.CommandType = CommandType.StoredProcedure;// 这里指定command的类型是存储过程
/* 下面的两句是新增加两个参数(存储过程需要的参数) */
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;
/* 以下两句是将两个参数添加到 command 中 */
da.SelectCommand.Parameters.Add(pa);
da.SelectCommand.Parameters.Add(pa1);


========================================================
其实楼主的代码可以这样写的:
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "proc_selectjiating";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.SqlParameter.Add("@jiatingname", SqlDbType.VarChar).Value=TextBox1.Text;
da.SelectCommand.SqlParameter.Add("@mima",SqlDbType.VarChar).Value=TextBox2.Text;

TAG:

 

评分:0

我来说两句

Open Toolbar