我有一个WinForm桌面应用程序,用于打印机碳粉库存。该数据库位于远程SQL Server上,仅由两个用于订单和交货的表组成,库存量的计算仅通过跟踪订单和交货的时间来进行。加上一堆带有制造商,型号,类别等的表格。
我具有用于存储订单表的存储过程:
CREATE PROCEDURE [dbo].[spINSERTCARICO] (
@MARCA VARCHAR(25),
@MODELLO VARCHAR(25),
@QTA INT,
@OPERATORE CHAR(7),
@CAUSALE VARCHAR(255))
AS
DECLARE @MODELLOESATTO VARCHAR(25)
SELECT @MODELLOESATTO = Modello FROM Modelli WHERE Modello = @MODELLO AND Marca = @MARCA
INSERT INTO Carico
(Modello
,Qta
,DataCarico
,Operatore
,Note)
VALUES
(@MODELLOESATTO
,@QTA
,GETDATE()
,@OPERATORE
,@CAUSALE)
正在通过此方法执行的:
public static int InsertCarico(string _marca, string _modello, string _causale, int _qta)
{
int insert = 0;
try
{
if (connection.State == ConnectionState.Closed) connection.Open();
using (SqlCommand cmd = new SqlCommand("spINSERTCARICO", connection))
{
cmd.Parameters.Add("@MARCA", SqlDbType.VarChar).Value = _marca;
cmd.Parameters.Add("@MODELLO", SqlDbType.VarChar).Value = _modello;
cmd.Parameters.Add("@QTA", SqlDbType.Int).Value = _qta;
cmd.Parameters.Add("@OPERATORE", SqlDbType.VarChar).Value = System.Environment.UserName;
cmd.Parameters.Add("@CAUSALE", SqlDbType.VarChar).Value = _causale;
cmd.ExecuteScalar();
}
return insert;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
我总是会收到错误
过程或函数'spINSERTCARICO'需要参数'@MARCA', 没有提供。
但是我真的不知道这是从哪里来的。调试时,我可以看到所有变量都已设置,并且在SSMS上手动进行操作时,
EXEC spINSERTCARICO 'OLIVETTI','B0987',1,'_MYPIDN','PROVA'
出什么问题了?
谢谢, 戴维德。
答案 0 :(得分:3)
CommandType
的默认值为文本。因此,SqlCommand
对象将提供的SP名称视为命令Text,直到未将其指定为StoredProcedure
为止。因此,将CommandType
设置为cmd.CommandType = CommandType.StoredProcedure
;
public static int InsertCarico(string _marca, string _modello, string _causale, int _qta)
{
int insert = 0;
try
{
if (connection.State == ConnectionState.Closed) connection.Open();
using (SqlCommand cmd = new SqlCommand("spINSERTCARICO", connection))
{
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@MARCA", SqlDbType.VarChar).Value = _marca;
cmd.Parameters.Add("@MODELLO", SqlDbType.VarChar).Value = _modello;
cmd.Parameters.Add("@QTA", SqlDbType.Int).Value = _qta;
cmd.Parameters.Add("@OPERATORE", SqlDbType.VarChar).Value = System.Environment.UserName;
cmd.Parameters.Add("@CAUSALE", SqlDbType.VarChar).Value = _causale;
insert = cmd.ExecuteScalar();
}
return insert;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
答案 1 :(得分:0)
执行以下两项更改:
cmd.CommandType = CommandType.StoredProcedure; // tell that's an SP
cmd.Parameters.Add("@MARCA", SqlDbType.VarChar).Value = _marca;
cmd.Parameters.Add("@MODELLO", SqlDbType.VarChar).Value = _modello;
cmd.Parameters.Add("@QTA", SqlDbType.Int).Value = _qta;
cmd.Parameters.Add("@OPERATORE", SqlDbType.VarChar).Value = System.Environment.UserName;
cmd.Parameters.Add("@CAUSALE", SqlDbType.VarChar).Value = _causale;
cmd.ExecuteNonQuery(); // don't expect any return value
答案 2 :(得分:0)
我最好的猜测是确保
_marca
不为null或为空,然后添加
cmd.CommandType = CommandType.StoredProcedure;
就在您的using语句下,不要期望返回值, 像这样
using (SqlCommand cmd = new SqlCommand("spINSERTCARICO", connection))
{
cmd.CommandType = CommandType.StoredProcedure; //Add this
cmd.Parameters.Add("@MARCA", SqlDbType.VarChar).Value = _marca;
cmd.Parameters.Add("@MODELLO", SqlDbType.VarChar).Value = _modello;
cmd.Parameters.Add("@QTA", SqlDbType.Int).Value = _qta;
cmd.Parameters.Add("@OPERATORE", SqlDbType.VarChar).Value =
System.Environment.UserName;
cmd.Parameters.Add("@CAUSALE", SqlDbType.VarChar).Value = _causale;
if(cmd.ExecuteNonQuery() > 0){
//Change to this
}
}
答案 3 :(得分:0)
请尝试以下代码-
public static int InsertCarico(string _marca, string _modello, string _causale, int _qta)
{
int insert = 0;
try
{
if (connection.State == ConnectionState.Closed) connection.Open();
using (SqlCommand cmd = new SqlCommand("spINSERTCARICO", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@MARCA", SqlDbType.VarChar).Value = _marca;
cmd.Parameters.Add("@MODELLO", SqlDbType.VarChar).Value = _modello;
cmd.Parameters.Add("@QTA", SqlDbType.Int).Value = _qta;
cmd.Parameters.Add("@OPERATORE", SqlDbType.VarChar).Value = System.Environment.UserName;
cmd.Parameters.Add("@CAUSALE", SqlDbType.VarChar).Value = _causale;
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
您似乎错过了提供命令类型的机会。