C# 数据集、数据源和存储过程

C# DataSet, Data Source and Stored Procedure

  1. 所以我在存储过程中有这段代码:

    select * from myTable where email = @email
    

    假设 @emailnvarchar(50) 并且 email 是 table

  2. 中的一列
  3. 我有一个 DataSet 有上面的存储过程。

  4. 我有一个 DataGridView,它的数据源是使用存储过程的数据集。

我想在存储过程中向@email 传递一个值。这可能吗?

是的。假设 SqlCommand 对象 'command',只需将参数添加到它。参见 here

string Email="Email";
string email="somebody@hotmail.com";
command.Parameters.Add(new SqlParameter(Email, email));

当然,您的存储过程也需要期待参数。

CREATE PROCEDURE [dbo].[usp_YourStoredProc]

( @Email [nvarchar(50)] = NULL, . . .

你可以用这个

// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "ProcedureName";
command.CommandType = CommandType.StoredProcedure;
// Add the input parameter and set its properties.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@email";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = textEmail.Text;
// Add the parameter to the Parameters collection. command.Parameters.Add(parameter);