繁体   English   中英

sql server存储过程更新记录

[英]sql server stored procedure to update records

我试图创建一个存储过程,该存储过程从程序中获取值并将其插入表中的特定列中。 我在存储过程中不断收到语法错误的错误,但无法弄清楚为什么? 程序中的代码:

            commandObj.CommandText = "sp_insert_profileDetails"

            commandObj.Parameters.Clear()

            Dim m_param As SqlParameter

            m_param = commandObj.Parameters.Add("@authorId", SqlDbType.NVarChar, 100)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = foundProfiles.Item(i)


            m_param = commandObj.Parameters.Add("@age", SqlDbType.NVarChar, 50)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = faceProfileAge.Item(i)

            m_param = commandObj.Parameters.Add("@gender", SqlDbType.NVarChar, 50)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = faceProfileSex.Item(i)

            m_param = commandObj.Parameters.Add("@locale", SqlDbType.NVarChar, 50)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = faceProfileLocation.Item(i)

            m_param = commandObj.Parameters.Add("@pic", SqlDbType.NVarChar, 100)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = faceProfilePic.Item(i)

            Dim recordsAffected As Integer = commandObj.ExecuteNonQuery
            Return recordsAffected



ALTER procedure [dbo].[sp_insert_profileDetails]
@authorId nvarchar(100),
@age nvarchar(50),
@gender nvarchar(50),
@locale nvarchar(50),
@pic nvarchar(100)

AS 
  Begin
  set nocount on
  update [dbo].ProfileFeed
  set [age] = @age,
      [sex] = @gender,
      [locale] = @locale,
      [pic] = @pic
  where 
      [authorId] = @authorId
  end

任何帮助表示赞赏吗?

我确实缺少几行代码,但是....

您是否指定commandObj.CommandType为System.Data.CommandType.StoredProcedure?

如果忘记设置,默认情况下,.NET会将其视为System.Data.CommandType.Text,并且数据库将引发不正确的语法错误。

我看不到您实际上在哪里在数据库中插入任何记录。

存储过程指出:

  update [dbo].ProfileFeed
  set [age] = @age,
      [sex] = @gender,
      [locale] = @locale,
      [pic] = @pic
  where 
      [authorId] = @authorId
  end

但是,SP的名称为[dbo].[sp_insert_profileDetails]

您是要写“ insert into [dbo].ProfileFeed ”而不是“ update ”吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM