使用sql中的过程向Giridview添加新行 [英] Add a new row to Giridview use a procedure in sql
本文介绍了使用sql中的过程向Giridview添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Procedure_SELECT向网格视图添加一行,但每次只显示一行。
System.Data.DataTable ServiceInsuranceList;
< pre lang =cs>< pre lang =cs> private void BtnSave_Click(object sender,EventArgs e)
{
int ServiceID = Convert.ToInt32(CmbService.SelectedValue);
int InsuranceId = Convert.ToInt32(CmbInsurance.SelectedValue);
if(ServiceInsuranceList == null)
{
ServiceInsuranceList = new System.Data.DataTable();
ServiceInsuranceList = HLP.Business.Object.Services.ServiseInsuranceList(InsuranceId,ServiceID);
GrdServiceInsurance.DataSource = ServiceInsuranceList;
}
else
{
ServiceInsuranceList.NewRow();
ServiceInsuranceList.AcceptChanges();
ServiceInsuranceList = HLP.Business.Object.Services.ServiseInsuranceList(InsuranceId,ServiceID);
if(ServiceInsuranceList!= null)
GrdServiceInsurance.DataSource = ServiceInsuranceList;
}
}& lt; / pre& gt;< / pre>
public static DataTable ServiseInsuranceList(int InsuranceID,int ServiceID)
{
DataTable PoseDataTable = new DataTable();
SqlCommand Command = HLP.Data.DataProvider.GenerateCommand(Clinic_ServiceInsuranceList,CommandType.StoredProcedure);
Command.Parameters.AddWithValue(@ InsuranceID,InsuranceID);
Command.Parameters.AddWithValue(@ ServiceID,ServiceID);
尝试
{
Command.Connection.Open();
Command.ExecuteNonQuery();
PoseDataTable = HLP.Data.DataProvider.GenerateDataTable(Command,CommandType.StoredProcedure);
}
catch(exception ex)
{
}
最后
{
HLP.Data.DataProvider.DisposeCommand(命令);
}
返回PoseDataTable;
}
我的程序:
ALTER PROC [dbo]。[Clinic_ServiceInsuranceList]
@ServiceID INT,
@InsuranceID INT
as
BEGIN
SELECT Service.Title,Service.Code,ActServiceost.Cost,InsuranceServiceCost.Cost AS Expr1,InsuraceCost.InsuraceID
FROM InsuraceCost INNER JOIN
InsuranceServiceCost ON InsuraceCost.ID = InsuranceServiceCost.InsuraceCostID LEFT OUTER JOIN
服务ON InsuranceServiceCost.ServiceID = Service.ID FULL OUTER JOIN
ActCostService ON Service.ID = ActCostService.ServiceID
其中
dbo.Service.ID = @ ServiceID和
dbo.InsuraceCost.InsuraceID=@InsuranceID
end
解决方案
该行为的原因在于此行:
其中 dbo.Service.ID=@ServiceID 和 dbo.InsuraceCost.InsuraceID = @ InsuranceID
如果您想显示所有记录,请删除where
语句或其中一个条件。
I use the "Procedure_SELECT" to add a row to the grid view, but each time a new row is displayed only.
System.Data.DataTable ServiceInsuranceList; <pre lang="cs"><pre lang="cs">private void BtnSave_Click(object sender, EventArgs e) { int ServiceID = Convert.ToInt32(CmbService.SelectedValue); int InsuranceId = Convert.ToInt32(CmbInsurance.SelectedValue); if (ServiceInsuranceList == null) { ServiceInsuranceList = new System.Data.DataTable(); ServiceInsuranceList = HLP.Business.Object.Services.ServiseInsuranceList(InsuranceId, ServiceID); GrdServiceInsurance.DataSource = ServiceInsuranceList; } else { ServiceInsuranceList.NewRow(); ServiceInsuranceList.AcceptChanges(); ServiceInsuranceList = HLP.Business.Object.Services.ServiseInsuranceList(InsuranceId, ServiceID); if (ServiceInsuranceList != null) GrdServiceInsurance.DataSource = ServiceInsuranceList; } }</pre></pre>
public static DataTable ServiseInsuranceList(int InsuranceID, int ServiceID) { DataTable PoseDataTable = new DataTable(); SqlCommand Command = HLP.Data.DataProvider.GenerateCommand("Clinic_ServiceInsuranceList", CommandType.StoredProcedure); Command.Parameters.AddWithValue("@InsuranceID", InsuranceID); Command.Parameters.AddWithValue("@ServiceID", ServiceID); try { Command.Connection.Open(); Command.ExecuteNonQuery(); PoseDataTable = HLP.Data.DataProvider.GenerateDataTable(Command, CommandType.StoredProcedure); } catch (Exception ex) { } finally { HLP.Data.DataProvider.DisposeCommand(Command); } return PoseDataTable; }
MY PROCEDURE:
ALTER PROC [dbo].[Clinic_ServiceInsuranceList] @ServiceID INT , @InsuranceID INT as BEGIN SELECT Service.Title, Service.Code, ActCostService.Cost, InsuranceServiceCost.Cost AS Expr1, InsuraceCost.InsuraceID FROM InsuraceCost INNER JOIN InsuranceServiceCost ON InsuraceCost.ID = InsuranceServiceCost.InsuraceCostID LEFT OUTER JOIN Service ON InsuranceServiceCost.ServiceID = Service.ID FULL OUTER JOIN ActCostService ON Service.ID = ActCostService.ServiceID where dbo.Service.ID=@ServiceID and dbo.InsuraceCost.InsuraceID=@InsuranceID end
解决方案
The reason of that behaviour is in this line:
where dbo.Service.ID=@ServiceID and dbo.InsuraceCost.InsuraceID=@InsuranceID
If you would like to display all records, removewhere
statement or one of the condition.
这篇关于使用sql中的过程向Giridview添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文