【发布时间】:2014-04-11 02:23:18
【问题描述】:
我正在尝试将INSERT 我的数据放入两个表Services 和Service-line
解释我的存储过程:
- 它检查表单中的现有 ID
-
如果未找到,它将在
Services表中创建一条新记录,并在 Form 中使用信息,然后使用scope_identity获取 ID 并为Service-Line表使用ID。ID 也会返回到 Form 并保持不变。
稍后在
Service-Line中插入第二条记录时,存储过程会检查现有的ID;如果找到,这一次,它会从表单中获取ID并在Service-Line中使用它
这是我的存储过程
请多多包涵,因为我正在编写这段代码并进行测试,很多行都被注释掉了
ALTER PROCEDURE [dbo].[InsertServiceServiceLine] (
--Services Entry
--FOR IF CONDITION ---CHECK THE DEFAULT VALUE IN THE FORM on SID
@ExistingSID int,
--SEELCT PARAMETES DEF VALUES
@ComboBoxSelectedBike varchar(100),
-- INPUT PARAMETERES FOR NEW RECORD
@CID int,
@Status bit = 1,
@CurrentMeter int,
@Labor decimal(20,0),
@GrandTotal decimal(20,0) = ISNULL,
--@NextService datetime,
--Service Line
@Spare nvarchar(500),
@Quantity int,
@Uprice decimal(20,2),
@Subtotal decimal(20,2)
)
AS
BEGIN
IF (@ExistingSID <= 0)
BEGIN
SET NOCOUNT ON;
DECLARE @BikeID int
SELECT @BikeID = (SELECT BikeID FROM TblBikeNames WHERE BikeName = @ComboBoxSelectedBike)
INSERT INTO [AutoDB_Sample].[dbo].[TblServices]
(CID,BikeID,Status,CurrentMeter,Labor,DateOfService)
VALUES
(@CID,@BikeID,@Status,@CurrentMeter,@Labor,GETDATE())
DECLARE @SID int
SET @SID = SCOPE_IDENTITY()
INSERT INTO [AutoDB_Sample].[dbo].[TblServiceLine]
(SID,Spare,Quantity,Uprice,Subtotal,DateCreated)
VALUES
(@SID,@Spare,@Quantity,@Uprice,@Subtotal,GETDATE())
RETURN @SID
END
ELSE
BEGIN
INSERT INTO [AutoDB_Sample].[dbo].[TblServiceLine]
(SID,Spare,Quantity,Uprice,Subtotal,DateCreated)
VALUES
(@ExistingSID,@Spare,@Quantity,@Uprice,@Subtotal,GETDATE())
END
END
当我在 C# Windows 窗体中使用此存储过程时出现错误
过程或函数的参数太多
这是错误截图
我认为将我的登录信息放在 SP 中会很棒并且可以提高我的应用程序的性能。但是现在卡住了。
这是我的 C# 代码
public void AddItemIntoServices_ServiceLine()
{
ConnectionStringSettings consetting = ConfigurationManager.ConnectionStrings["AutoDB"];
String ConnectionString = consetting.ConnectionString;
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open(); // open the connection
// Specify the name of the Stored Procedure you will call
String SP_Name = "InsertServiceServiceLine";
// Create the SQL Command object and specify that this is a SP.
SqlCommand cmd = new SqlCommand(SP_Name, con);
cmd.CommandType = CommandType.StoredProcedure;
// Specify values for the input parameters of our Stored Procedure
// Parameters MUST be named the same as the parameters defined in the Stored Procedure.
//~~ If Condition Parameter ****************************************************************************~~//
int exitstingSID;
if (int.TryParse(LblSID_Data.Text, out exitstingSID)) ;
SqlParameter ExistingSID = new SqlParameter("@ExistingSID", exitstingSID);
ExistingSID.Direction = ParameterDirection.Input;
ExistingSID.DbType = DbType.Int16;
cmd.Parameters.Add(ExistingSID);
//Parameter to select Bike ID from Selected Bike Name
SqlParameter ParamBikeID = new SqlParameter("@ComboBoxSelectedBike", ComboBx_BikeNames.Text);
ParamBikeID.Direction = ParameterDirection.Input;
ParamBikeID.DbType = DbType.String;
cmd.Parameters.Add(ParamBikeID);
//~~ Customer Info ************************************************************************************~~//
//CID Convertion
int P_CID;
if (int.TryParse(LblCID_Data.Text, out P_CID)) ;
cmd.Parameters.AddWithValue("@CID", P_CID);
cmd.Parameters.AddWithValue("@Cname", this.TxtBx_CustomerName.Text);
cmd.Parameters.AddWithValue("@Vnum", this.TxtBx_VehicleNumber.Text);
//~~ Service Info ************************************************************************************~~//
//Labor Convertion
int Laborint;
if (int.TryParse(TxtBxLabor.Text, out Laborint)) ;
SqlParameter ParamLabor = new SqlParameter("@Labor", Laborint);
ParamLabor.Direction = ParameterDirection.Input;
ParamLabor.DbType = DbType.Int16;
cmd.Parameters.Add(ParamLabor);
//CurrentMeterConversion
int currentMeterint;
if (int.TryParse(TxtBx_CurrentMeter.Text, out currentMeterint)) ;
SqlParameter ParamCurrentMeter = new SqlParameter("@CurrentMeter", currentMeterint);
ParamCurrentMeter.Direction = ParameterDirection.Input;
ParamCurrentMeter.DbType = DbType.Int16;
cmd.Parameters.Add(ParamCurrentMeter);
//Return Value
SqlParameter ParamReturn = new SqlParameter("@SID", SqlDbType.Int);
ParamReturn.Direction = ParameterDirection.Output;
ParamReturn.DbType = DbType.Int16;
cmd.Parameters.Add(ParamReturn);
//~~ Service Info ************************************************************************************~~//
//Converstions
Decimal UP, ST;
if (Decimal.TryParse(TxtBx_UnitPrice.Text, out UP)) ;
if (Decimal.TryParse(TxtBxTotal.Text, out ST)) ;
//SpareName
cmd.Parameters.AddWithValue("@Spare", ComboBx_SparesName.Text);
//Quantity
cmd.Parameters.AddWithValue("@Qty", NumericBx_Quantity.Value);
//Unit Price
SqlParameter ParamUp = new SqlParameter("@Uprice", UP);
ParamUp.Direction = ParameterDirection.Input;
ParamUp.DbType = DbType.Decimal;
cmd.Parameters.Add(ParamUp);
//Total
SqlParameter ParamTot = new SqlParameter("Subtotal", ST);
ParamTot.Direction = ParameterDirection.Input;
ParamTot.DbType = DbType.Decimal;
cmd.Parameters.Add(ParamTot);
cmd.ExecuteNonQuery();
String _returnedSID = cmd.Parameters["@SID"].Value.ToString();
LblSID_Data.Text = _returnedSID;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
clear();
ToolStripLable_Status.Text = "New Service Record Created";
}
}
【问题讨论】:
-
我认为问题出在调用这个sp的C#代码中。请将该代码添加到您的问题中。
-
消息清晰,提供更多参数给SP。检查调用该 SP 的代码所在的参数。
-
@史蒂夫。 . .我已经添加了 C# 代码。
-
@ThitLwinOo 。 . SP 在 SSMS 中运行良好,当我从 C# 执行它时出现此错误
标签: c# sql-server winforms function stored-procedures