我正在尝试在我的 C# 应用程序中调用存储过程,但它并没有真正调用我尝试了很多尝试调用它ExecuteReader
,等等NonReader
,Scalar
但它仍然没有被执行我在下面显示我的整个代码,包括我曾经使用过的 sql 命令
请检查我的代码中真正的问题是什么
提前致谢
Stock
桌子
Create Table Stock
(
--S_ID int Primary Key identity (1,1),
Date_of_Added Datetime Default (getdate()),
EnvType nvarchar(10),
TypeSize nvarchar(10),
Size nvarchar(10),
[Description] sql_variant,
Qty int,
Quality int,
)
NewProduct
桌子
Create Table NewProduct
(
MS nvarchar(30),
Date_of_Added Datetime Default (getdate()),
Invoice_No nvarchar(20),
Challan_No nvarchar(20),
EnvType nvarchar(10),
TypeSize nvarchar(10),
Size nvarchar(10),
Description sql_variant,
Qty int,
Quality int,
PayState nvarchar(10),
Balanace_Amount money,
PaymentMode nvarchar(6),
Cheque_No int,
BankDetails nvarchar(30),
P_UnitPrice money,
P_Price money
)
-- Creating trigger of above stored procedure to update or insert the stock as needed
Create Procedure StockProc
(@EnvType nvarchar(50),@TypeSize nvarchar(50),@Size nvarchar(50),@Desc Sql_variant,@Qty int,@Quality int)
As Begin
if exists (select S.EnvType, S.TypeSize, S.Size, S.[Description], S.Qty, S.Quality
from NewProduct NP Full Join Stock S
on(NP.Size=NP.Size)
where NP.Size=@Size and NP.TypeSize=@TypeSize and NP.EnvType=@EnvType and NP.Quality=@Quality)
--Print 'Record Found';
--If Record will found then ut will update the quantity
Begin
update Stock
set Qty=NP.Qty+S.Qty
from NewProduct NP, Stock S
--have not used IN Operator here as IN works as OR Clause
where S.EnvType=NP.EnvType and S.TypeSize=NP.TypeSize and S.Size=NP.Size
End
Else
--If no record will match with the enterd data, New Record will be added on Stock Table
Begin
--Print 'No Record Found'
insert into Stock (EnvType, TypeSize, Size, [Description], Qty, Quality)
values(@EnvType,@TypeSize,@Size,@Desc,@Qty,@Quality)
--Below Lines will slow down the performance so we are using the above query
--Truncate Table Stock
--insert into Stock (Date_of_Added, EnvType, TypeSize, Size, [Description], Qty, Quality)
--select Date_of_Added, EnvType, TypeSize, Size, [Description], Qty, Quality from NewProduct
End
End
--虽然在sql中测试它工作正常,你可以通过下面的执行代码来测试它
exec StockProc 'fdfdf','sdf','dsfs','sdf',4,5
C#代码:
private void cmdSave_Click(object sender, EventArgs e)
{
try
{
SqlCommand cmd = null;
//Will check the connection state if got open then will close the connection
if (ObjCon.con.State == ConnectionState.Open)
ObjCon.con.Close();
ObjCon.con.Open();
string Query = "INSERT INTO NewProduct ([MS], [Date_of_Added], [Invoice_No], [Challan_No], [EnvType], [TypeSize], [Size],[Description], [Qty], [Quality], [PayState], [Balanace_Amount], [PaymentMode], [Cheque_No], [BankDetails], [P_UnitPrice], [P_Price]) VALUES ('" + txtMs.Text + "','" + dtpNewProduct.Value + "','" + txtInvoiceNo.Text + "','" + txtChallanNo.Text + "','" + cboType.Text + "','" + txtTypeSize1.Text + "X" + txtTypeSize2.Text + "','" + txtSize1.Text + "X" + txtSize2.Text + "','" + txtDesc.Text + "','" + nudQty.Value + "','" + nudQuality.Value + "','" + cboPayState.Text + "','" + txtBalAmt.Text + "','" + cboPayMode.Text + "','" + txtChequeNo.Text + "','" + txtBankNameBranch.Text + "','" + txtPUPrice.Text + "','" + txtPPrice.Text + "')";
cmd = new SqlCommand(Query, ObjCon.con);
cmd.ExecuteNonQuery();
SqlCommand cmd2 = new SqlCommand("StockProc", ObjCon.con);
//SqlCommand cmd2 = new SqlCommand("exec StockProc '" + cboType.Text + "','" + txtTypeSize1.Text + "X" + txtTypeSize2.Text + "','" + txtSize1.Text + "X" + txtSize2.Text + "','" + txtDesc.Text + "','" + nudQty.Value + "','" + nudQuality.Value + "'", ObjCon.con);
cmd2.CommandType = CommandType.StoredProcedure;
//cmd2.Parameters.Add(new SqlParameter("@Date_of_Added", dtpNewProduct.Value));
cmd2.Parameters.Add(new SqlParameter("@EnvType", cboType.Text));
cmd2.Parameters.Add(new SqlParameter("@TypeSize", txtTypeSize1.Text + "X" + txtTypeSize2.Text));
cmd2.Parameters.Add(new SqlParameter("@Size", txtSize1.Text + "X" + txtSize2.Text));
cmd2.Parameters.Add(new SqlParameter("@Desc", txtDesc.Text));
cmd2.Parameters.Add(new SqlParameter("@Qty", nudQty.Value));
cmd2.Parameters.Add(new SqlParameter("@Quality", nudQuality.Value));
//http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/Callstoredprocedureandpassintheparameter.htm
//cmd2.Parameters.Add(new SqlParameter("@EnvType", SqlDbType.NVarChar)).Value = cboType.Text;
//cmd2.Parameters.Add(new SqlParameter("@TypeSize", SqlDbType.NVarChar)).Value = txtTypeSize1.Text + "X" + txtTypeSize2.Text;
//cmd2.Parameters.Add(new SqlParameter("@Size", SqlDbType.NVarChar )).Value=txtSize1.Text + "X" + txtSize2.Text;
//cmd2.Parameters.Add(new SqlParameter("@Desc", SqlDbType.Variant)).Value=txtDesc.Text;
//cmd2.Parameters.Add(new SqlParameter("@Qty",SqlDbType.Int)).Value= nudQty.Value;
//cmd2.Parameters.Add(new SqlParameter("@Quality", SqlDbType.Int)).Value = nudQuality.Value;
//SqlDataReader dr = cmd2.ExecuteReader();
//if (dr.HasRows)
// MessageBox.Show("FOUND")
//if (dr.IsClosed == false)
//{
// dr.Close();
//}
//dr.Read();
//cmd2.ExecuteScalar();
//cmd2.ExecuteNonQuery();
//set Asynchronous Processing=true in conneciton String in order to use BeginExecureNonQuery Method
//cmd2.BeginExecuteNonQuery();
//if (cmd2.ExecuteNonQuery() > 0)
// MessageBox.Show("FOUND");
//dr.Close();
ObjCon.con.Close();
MessageBox.Show("Your Details has been Saved\rThank You", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
catch (Exception ex)
{
MessageBox.Show("The Following Error Occur" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}
请帮忙......
或者你认为我应该使用 UDF 而不是存储过程