C# 参数化存储过程在C语言中的应用#
我试图在我的C#应用程序中调用存储过程,但它并没有真正调用。我尝试了很多次调用它C# 参数化存储过程在C语言中的应用#,c#,stored-procedures,ado.net,user-defined-functions,sql-parametrized-query,C#,Stored Procedures,Ado.net,User Defined Functions,Sql Parametrized Query,我试图在我的C#应用程序中调用存储过程,但它并没有真正调用。我尝试了很多次调用它ExecuteReader,NonReader,Scalar等。但它仍然没有被执行。我在下面显示了我的全部代码,包括我曾经使用过的sql命令 请检查我的代码中到底是什么问题 提前谢谢 库存表格 Create Table Stock ( --S_ID int Primary Key identity (1,1), Date_of_Added Datetime Default (getdate()), EnvType n
ExecuteReader
,NonReader
,Scalar
等。但它仍然没有被执行。我在下面显示了我的全部代码,包括我曾经使用过的sql命令
请检查我的代码中到底是什么问题
提前谢谢
库存
表格
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,
)
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
新产品
表格
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,
)
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代码进行测试
exec StockProc 'fdfdf','sdf','dsfs','sdf',4,5
C#代码:
请帮忙
或者你认为我应该在c#Code中使用UDF而不是存储过程,检查“String query”,它在值之后被错误地写入
e、 g.在使用cmd参数时,使用@ms而不是控件名。首先,我相信错误在您的存储过程中,
StockProc
。使用此更改(我在这里占卜意图,所以您可能需要调整它):
现在,来看看你的C代码。根据我的意见,遵循a.s.a.p.处理连接的最佳实践:
private void cmdSave_Click(object sender, EventArgs e)
{
try
{
using (var con = new SqlConnection(ObjCon.con.ConnectionString))
{
// I assume this was just for testing to show a direct INSERT works when the sproc didn't.
// Otherwise, I'd be using replaceable parameters rather than straight text values as that
// makes it quite vulnerable to SQL injection attacks. Better yet, make this a sproc as well
// and use it rather than client-side SQL.
var 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 + "')";
using (var cmd = new SqlCommand(query, con))
{
con.Open();
cmd.ExecuteNonQuery();
}
}
using (var con = new SqlConnection(ObjCon.con.ConnectionString))
using (var cmd2 = new SqlCommand("StockProc", 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;
con.Open();
cmd2.ExecuteNonQuery();
}
MessageBox.Show("Your Details has been Saved\rThank You", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
catch (Exception ex)
{
MessageBox.Show("The Following Error Occurred" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}
“它不是真正的调用”并不是对正在发生的事情的一个很好的描述。你有例外吗?如果是这样,那又怎样?目前,您提供的代码没有试图调用它。您添加了一组参数,仅此而已。您已经注释掉了
ExecuteNonQuery
etc调用。如果使用ExecuteNonQuery
,会发生什么情况?什么都没有,只执行第一个查询,存储过程没有响应,但是如果删除任何参数,我会直接得到一个异常,即缺少“指定的参数名”…首先,没有人调用Jon Skeet“dude”。其次为什么不编辑您提供的代码示例以反映您实际尝试过的内容,而不是提供注释过的代码,然后努力传达您实际尝试过的内容呢?我也有点担心ObjCon.con
-它看起来像一个连接对象,其生存期未知,并且在任何给定时间都不会假定其状态。最佳做法是在需要时创建连接对象(例如,就在创建和调用命令对象之前),并在完成后立即将其处置。如果连接字符串没有更改(不太可能!),则连接本身将由运行时池化,并且不会招致任何惩罚。这很好,即使hat查询也可以正常工作。我在存储过程中收到一个错误。您忘记在服务器cmd2上执行。ExecuteOnQuery();您可以看到cmd2.ExecuteNonQuery();我的意思是说我也尝试过……尝试这种方法(不带“@”):cmd2.Parameters.Add(new-SqlParameter(“EnvType”,cboType.Text));你可以看到注释重要代码引起的混乱,我想是吧?也许不是,因为它被指出了,而你没有纠正它。你为什么不现在就停止,确切地说明什么是不能正常工作的?