c#中使用的sql存储过程

时间:2014-04-01 07:53:11

标签: c# asp.net sql stored-procedures sql-server-2008-r2

我在sql中使用了以下存储过程:

alter procedure [dbo].[usp_Member_Org_OnGoingJobs]
(
    @idUser varchar(50)

)
as
begin
declare @qry as varchar(max)
set @qry='select J.idJob,j.DateAdded,j.OpenedByWho,j.JobAddress ,j.final,j.idOrg,j.note
                              from Job J 
                              inner join Users U on
                              U.idOrg=J.idOrg
                              where U.IdUser='+ @idUser+ '
                              and ISNULL(j.Final,'')='' 
                              order by idJob'


  execute(@qry)
end
GO

此存储过程在sql中成功形成。

但是,当我试图通过asp.net c#使用它时,它给了我错误:

Incorrect syntax near the keyword 'order'.

一切似乎都是正确的。

请告诉我我在哪里弄错了?

编辑:

private void BindOnGoingJobs()
    {
        string sqlOnGoingJobs = "usp_Member_Org_OnGoingJobs";

        DataTable dtJobList = new DataTable();
        ArrayList paramList = new ArrayList();

        paramList.Add(new ParamData("@idUser", Convert.ToString(Session["idUser"])));
        dtJobList = obj.ExecuteProcedureAndGetDataTable(sqlOnGoingJobs, paramList);
        grdOnGoingJobs.DataSource = dtJobList;
        grdOnGoingJobs.DataBind();

        paramList.Clear();
    }

 public DataTable ExecuteProcedureAndGetDataTable(string procedureName, ArrayList Parameters)
        {
            DataTable dt = new DataTable();
            try
            {
                if (con.State != ConnectionState.Open)
                    con.Open();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = procedureName;
                cmd.Parameters.Clear();
                foreach (ParamData p in Parameters)
                {
                    cmd.Parameters.AddWithValue(p.pName, p.pValue);
                }
                da.SelectCommand = cmd;
                da.Fill(dt);
                con.Close();
                return dt;
            }
            catch (Exception ex)
            {
                con.Close();
                return dt;
            }
        }

3 个答案:

答案 0 :(得分:4)

您需要在ISNULL检查周围加倍单引号

set @qry='select J.idJob,j.DateAdded,j.OpenedByWho,j.JobAddress ,j.final,j.idOrg,j.note
                          from Job J 
                          inner join Users U on
                          U.idOrg=J.idOrg
                          where U.IdUser='+ @idUser+ '
                          and ISNULL(j.Final,'''')='''' 
                          order by idJob'

答案 1 :(得分:1)

你需要通过两次这样的引号来转义引号。

  

and ISNULL(j.Final,'''')=''''

查看此博文http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/

答案 2 :(得分:0)

按顺序添加表别名,我想应该解决问题

由J.idJob命令