c#存储过程不起作用

时间:2016-08-25 08:49:32

标签: c# sql sql-server stored-procedures

我使用存储过程来更新表,但它不起作用。 这是:

ALTER PROCEDURE [dbo].[updateRessourceLigneFab]
-- Add the parameters for the stored procedure here
@CodeArticle nvarchar(300),
@Division nvarchar(300),
@Colonne nvarchar(300),
@Valeur nvarchar(300),
@IDres nvarchar(300)
AS BEGIN SET NOCOUNT ON;
update dbo.[Ressources PDP] set @Colonne = @Valeur where [Code article] = @CodeArticle and Division = @Division and LigneFab = @IDres
END
GO

我在c#中调用我的存储过程,如下所示:

        public static DataTable updateRessourceLigneFab(string CodeArticle, string Division, string Colonne, string Valeur, string IDres)
    {
        String querystring = "[TipiakTTP].[dbo].[updateRessourceLigneFab]";
        var lsParam = new List<SqlParameter>();
        AccesBase.ajouteParametre(ref lsParam, CodeArticle, "@CodeArticle");
        AccesBase.ajouteParametre(ref lsParam, Division, "@Division");
        AccesBase.ajouteParametre(ref lsParam, Colonne, "@Colonne");
        AccesBase.ajouteParametre(ref lsParam, Valeur, "@Valeur");
        AccesBase.ajouteParametre(ref lsParam, IDres, "@IDres");
        return AccesBase.execute(querystring, lsParam);
    }

这是我用来调用调用我的存储过程的函数的代码:

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        var grid = (DataGridView)sender;
        if (grid[e.ColumnIndex, e.RowIndex].Value.ToString() != "")
        {
            string codearticle =  grid[4, 0].Value.ToString() ;
            string division = grid[3, 0].Value.ToString() ;
            int y = int.Parse(getWeekNumberIncrement(Week)) - int.Parse(getDateNow())+1;
            string val = "ress" + y.ToString();
            if (y < 10)
            {
                 val = "ress0" + y.ToString();
            }
            string colonne =  "'"+ val +"'" ;
            string valeur =  grid[0, 0].Value.ToString() ;
            string ligne =  grid[5, 0].Value.ToString() ;
            DataTable datagrid1 = Controller.Version.ControllerChoixVersion.getRessourceLigneFab(codearticle, division, ligne);
            if ( datagrid1.Rows.Count == 0)
            {
                Controller.Version.ControllerChoixVersion.insertRessourceLigneFab(codearticle, division, ligne);
            }
            Controller.Version.ControllerChoixVersion.updateRessourceLigneFab(codearticle, division, colonne, valeur, ligne);
        }

问题在于,我不知道为什么我的桌子没有更新,我尝试了很多东西,我还在努力解决它..

有关此sql语句的信息:

    update dbo.[Ressources PDP] set ress01 = '111' where [Code article] = '10000100510' and Division = '1000' and LigneFab = 'RECONDI'

我确信这很简单,但我找不到。

感谢您的帮助

更新/解决方案:

使用我的存储过程中的上述代码完成工作:

ALTER PROCEDURE [dbo].[updateRessourceLigneFab]
-- Add the parameters for the stored procedure here
@CodeArticle nvarchar(300),
@Division nvarchar(300),
@Colonne nvarchar(300),
@Valeur nvarchar(300),
@IDres nvarchar(300)
AS BEGIN DECLARE @SQLString NVARCHAR(500); SET NOCOUNT ON;

        SET @SQLString = 'update dbo.[Ressources PDP] set '+@Colonne+' = '+@Valeur+' where [Code article] = '+@CodeArticle+' and Division = '+@Division+' and LigneFab = '+@IDres+'';
EXECUTE sp_executesql @SQLString
END
GO

1 个答案:

答案 0 :(得分:0)

问题在于本声明

update dbo.[Ressources PDP] set @Colonne = @Valeur where [Code article] = @CodeArticle and Division = @Division and LigneFab = @IDres

你不能设置@ Colonne = @ Valeur