我有一系列輸入源,我想用它來執行我的sql數據庫上的存儲過程。存儲過程如下所示:我怎樣才能得到這個執行?
USE [InvoiceSHC]
GO
/****** Object: StoredProcedure [dbo].[UpdateSHCInvoice] Script Date: 02/21/2013 12:03:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[UpdateSHCInvoice]
-- Add the parameters for the stored procedure here
@Ref nvarchar(50),
@PhaseName nvarchar(50),
@PageType nvarchar(50),
@Page float,
@Percent nvarchar(50),
@ChngType nvarchar(50),
@RowCount int output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into MasterData2 ([Ad],[Phase],[Page Type] , [Page Rate], [Percent] , [Change Type])
values(@Ref, @PhaseName , @PageType , @Page , @Percent ,@ChngType);
/* @@ROWCOUNT returns the number of rows that are affected by the last statement. */
select @RowCount = @@ROWCOUNT
END
我在窗體上有一個按鈕,提示用戶更新表。這是它的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void MasterData_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=(local);database=InvoiceSHC;Trusted_Connection=Yes");
SqlDataAdapter DA = new SqlDataAdapter("UpdateSHCInvoice", con);
SqlCommand cmd = new SqlCommand("UpdateSHCInvoice", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter Ref = cmd.Parameters.Add("@Ref", SqlDbType.NVarChar, 50);
SqlParameter Phase = cmd.Parameters.Add("@Phase", SqlDbType.NVarChar, 50);
SqlParameter PageType = cmd.Parameters.Add("@PageType", SqlDbType.NVarChar, 50);
SqlParameter Page = cmd.Parameters.Add("@Page", SqlDbType.NVarChar, 50);
SqlParameter Percent = cmd.Parameters.Add("@Percent", SqlDbType.Float);
SqlParameter ChngType = cmd.Parameters.Add("@ChngType", SqlDbType.NVarChar, 50);
Ref.Value = dlRef.SelectedItem;
Phase.Value = dlPhase.SelectedItem;
PageType.Value = dlPageType.SelectedItem;
Page.Value = tbPage.Text;
Percent.Value = dlPercent.SelectedItem;
ChngType.Value = dlChngType.SelectedItem;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
當我拿出cmd.ExecuteNonQuery()時,我得到沒有錯誤,也沒有結果。當我在那裏有結果是一個錯誤System.InvalidCastException:對象必須實現IConvertible。
我爲自己的無知事先道歉,我自學成才,每天都在學習新東西。在大約127年和4天內,我可能會理解這一點。
nunespascal - 非常感謝你,這個着名的作品。我需要最後一件事才能完成。一旦我完成插入,我有一個gridview(gridview1)需要根據屏幕上的信息刷新。可以將它們合併到相同的代碼中嗎? – 2013-02-21 19:06:04
如果你的gridview已經綁定到數據源,你只需要調用'DataBind' – nunespascal 2013-02-22 04:05:23