【问题标题】:C# DataSet, Data Source and Stored ProcedureC# 数据集、数据源和存储过程
【发布时间】:2015-03-16 13:24:21
【问题描述】:
  1. 所以我在存储过程中有这段代码:

    select * from myTable where email = @email
    

    假设@emailnvarchar(50) 并且email 是表中的一列

  2. 我有一个DataSet,它具有上述存储过程。

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

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

【问题讨论】:

  • 是的,通过带参数调用过程。你可以给它 C# 代码吗?
  • 是的,有可能。只需将适当的参数添加到您的 SqlCommand 或您用来获得结果的任何内容。如果没有看到您的实际代码示例,很难说更多。
  • 谷歌 C#.NET ADO 参数

标签: c# sql-server stored-procedures datagridview


【解决方案1】:

是的。假设 SqlCommand 对象“命令”,只需将参数添加到它。见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, . . .

【讨论】:

    【解决方案2】:

    你可以用这个

    // 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 2010-12-13
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多