使用数组中的两个参数调用存储过程
Call a stored procedure with two parameters in array
我想用来自存储过程 pp_sp_MachineAndOp
的数据填充组合框,其中有两个参数:
ALTER PROCEDURE [dbo].[pp_sp_MachineAndOp]
@MachineAndOpID int,
@Seq int
AS
BEGIN
SET NOCOUNT ON;
IF @MachineAndOpID = 0
--Show All
IF @Seq = 0
SELECT
mo.*,
o.Operation, o.Seq,
m.Machine
FROM
[dbo].[pp_MachineAndOp] mo
INNER JOIN
pp_Machines m ON m.MachineID = mo.MachineID
INNER JOIN
pp_vw_Operations o ON o.OperationID = mo.OperationID
WHERE
m.Active = 1
ORDER BY
mo.OperationID, MachinePriority
ELSE
--show all following operations
SELECT
mo.*,
o.Operation, o.Seq,
m.Machine
FROM
[dbo].[pp_MachineAndOp] mo
INNER JOIN
pp_Machines m ON m.MachineID = mo.MachineID
INNER JOIN
pp_vw_Operations o ON o.OperationID = mo.OperationID
WHERE
m.Active = 1 AND o.Seq > @Seq
ORDER BY
mo.OperationID, MachinePriority
ELSE
-- Show Selected
SELECT
mo.*,
o.Operation, o.Seq,
m.Machine
FROM
[dbo].[pp_MachineAndOp] mo
INNER JOIN
pp_Machines m ON m.MachineID = mo.MachineID
INNER JOIN
pp_vw_Operations o ON o.OperationID = mo.OperationID
WHERE
m.Active = 1
AND mo.MachineAndOpID = @MachineAndOpID
ORDER BY
mo.OperationID, MachinePriority
END
如果我在 SQL Server Management Studio 中执行它,它工作得很好。
在 C# 中,我有这个方法从存储过程中获取数据:
public static DataTable ExecuteDataTable(string storedProcedureName, params SqlParameter[] arrParam)
{
DataTable dt = new DataTable();
// Open the connection
using (SqlConnection sqlConn = new SqlConnection(strConn))
{
try
{
sqlConn.Open();
// Define the command
using (SqlCommand sqlCmd = new SqlCommand())
{
sqlCmd.Connection = sqlConn;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = storedProcedureName;
// Handle the parameters
if (arrParam != null)
{
foreach (SqlParameter param in arrParam)
{
sqlCmd.Parameters.Add(param);
}
}
// Define the data adapter and fill the dataset
using (SqlDataAdapter da = new SqlDataAdapter(sqlCmd))
{
da.Fill(dt);
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return dt;
}
如果我这样调用它,我不会收到任何错误,结果也符合预期:
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID", "0"),
new SqlParameter("@Seq", "0"));
如果我这样称呼它
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID", "0"),
new SqlParameter("@Seq", "7"));
我收到一个错误
Parameter @MachineAndOpID not supplied
我在这次通话中遇到同样的错误:
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID",0),
new SqlParameter("@Seq", 0));
这里:
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID",SqlDbType.BigInt, 0),
new SqlParameter("@Seq",SqlDbType.BigInt, 7));
我觉得它与数据类型有某种关系,但不知道如何解决它。
您必须定义正确的数据类型。
它应该像这样工作:
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID", SqlDbType.Int) { Value = 6 },
new SqlParameter("@Seq", SqlDbType.Int) { Value = 6 });
我想用来自存储过程 pp_sp_MachineAndOp
的数据填充组合框,其中有两个参数:
ALTER PROCEDURE [dbo].[pp_sp_MachineAndOp]
@MachineAndOpID int,
@Seq int
AS
BEGIN
SET NOCOUNT ON;
IF @MachineAndOpID = 0
--Show All
IF @Seq = 0
SELECT
mo.*,
o.Operation, o.Seq,
m.Machine
FROM
[dbo].[pp_MachineAndOp] mo
INNER JOIN
pp_Machines m ON m.MachineID = mo.MachineID
INNER JOIN
pp_vw_Operations o ON o.OperationID = mo.OperationID
WHERE
m.Active = 1
ORDER BY
mo.OperationID, MachinePriority
ELSE
--show all following operations
SELECT
mo.*,
o.Operation, o.Seq,
m.Machine
FROM
[dbo].[pp_MachineAndOp] mo
INNER JOIN
pp_Machines m ON m.MachineID = mo.MachineID
INNER JOIN
pp_vw_Operations o ON o.OperationID = mo.OperationID
WHERE
m.Active = 1 AND o.Seq > @Seq
ORDER BY
mo.OperationID, MachinePriority
ELSE
-- Show Selected
SELECT
mo.*,
o.Operation, o.Seq,
m.Machine
FROM
[dbo].[pp_MachineAndOp] mo
INNER JOIN
pp_Machines m ON m.MachineID = mo.MachineID
INNER JOIN
pp_vw_Operations o ON o.OperationID = mo.OperationID
WHERE
m.Active = 1
AND mo.MachineAndOpID = @MachineAndOpID
ORDER BY
mo.OperationID, MachinePriority
END
如果我在 SQL Server Management Studio 中执行它,它工作得很好。
在 C# 中,我有这个方法从存储过程中获取数据:
public static DataTable ExecuteDataTable(string storedProcedureName, params SqlParameter[] arrParam)
{
DataTable dt = new DataTable();
// Open the connection
using (SqlConnection sqlConn = new SqlConnection(strConn))
{
try
{
sqlConn.Open();
// Define the command
using (SqlCommand sqlCmd = new SqlCommand())
{
sqlCmd.Connection = sqlConn;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = storedProcedureName;
// Handle the parameters
if (arrParam != null)
{
foreach (SqlParameter param in arrParam)
{
sqlCmd.Parameters.Add(param);
}
}
// Define the data adapter and fill the dataset
using (SqlDataAdapter da = new SqlDataAdapter(sqlCmd))
{
da.Fill(dt);
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return dt;
}
如果我这样调用它,我不会收到任何错误,结果也符合预期:
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID", "0"),
new SqlParameter("@Seq", "0"));
如果我这样称呼它
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID", "0"),
new SqlParameter("@Seq", "7"));
我收到一个错误
Parameter @MachineAndOpID not supplied
我在这次通话中遇到同样的错误:
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID",0),
new SqlParameter("@Seq", 0));
这里:
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID",SqlDbType.BigInt, 0),
new SqlParameter("@Seq",SqlDbType.BigInt, 7));
我觉得它与数据类型有某种关系,但不知道如何解决它。
您必须定义正确的数据类型。
它应该像这样工作:
DataTable dtMO = Helper.ExecuteDataTable("pp_sp_MachineAndOp",
new SqlParameter("@MachineAndOpID", SqlDbType.Int) { Value = 6 },
new SqlParameter("@Seq", SqlDbType.Int) { Value = 6 });