日期:2014-05-20  浏览次数:20749 次

ADO.NET "存储过程" 若干问题求解答.
1. 请问"存储过程" 是不是用来向数据库中添加数据行的?
2. 以下代码摘自 MSDN:
C# code

    // Create the command and set its properties.
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandText = "SalesByCategory";
        command.CommandType = CommandType.StoredProcedure;

        // Add the input parameter and set its properties.
        SqlParameter parameter = new SqlParameter();
        parameter.ParameterName = "@CategoryName";
        parameter.SqlDbType = SqlDbType.NVarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = categoryName;

        // Add the parameter to the Parameters collection. 
        command.Parameters.Add(parameter);


(1) command.CommandText 的内容是否可以随意填写?
(2) parameter.ParameterName 的内容中, @ 后面接的那段字符串是什么东西?
(3) parameter.Direction 的值改成 ParameterDirection.Output, 还能正常执行 "存储过程" 吗?

------解决方案--------------------
(1)这是存储过程的名称,你定义了什么名称的存储过程,这里就写什么。
(2)这个是你定义的存储过程里的参数,必须一样。如果没定义可以不写。
(3)不可以,看你的参数是什么类型的。是out put类型的就可以。
------解决方案--------------------
1. 请问"存储过程" 是不是用来向数据库中添加数据行的?

不仅仅是添加数据的,其他的修改、删除等也是可以操作的。

2. 以下代码摘自 MSDN:
C# code

// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SalesByCategory";
command.CommandType = CommandType.StoredProcedure;

// Add the input parameter and set its properties.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;

// Add the parameter to the Parameters collection. 
command.Parameters.Add(parameter);


(1) command.CommandText 的内容是否可以随意填写?

不能,这个是你要用到的存储过程的名字,必须是某个数据库表中实际存在的。

(2) parameter.ParameterName 的内容中, @ 后面接的那段字符串是什么东西?

后面的那个是参数,如果你的存储过程中的参数

(3) parameter.Direction 的值改成 ParameterDirection.Output, 还能正常执行 "存储过程" 吗?

估计不行了,Oupput是输出参数,如果你的存储过程中有参数指定为output的话,那么你就需要指定该参数为输出参数。

------解决方案--------------------
1、存储过程可以做你想做的任何事情,当然是针对数据库的数据的。比如添加、删除、修改等等。都能做,而且存储过程由于是预编译好的,所以执行效率很高,。