【问题标题】:Stored Procedure Not working from c# code存储过程不适用于 c# 代码
【发布时间】:2014-08-31 17:04:20
【问题描述】:

我正在尝试在 C# 中调用存储过程。

using (SqlConnection connection = new SqlConnection(connectionString))
   using (SqlCommand command = connection.CreateCommand())
    {
      command.CommandType = CommandType.StoredProcedure;
      command.CommandText = "updateData";
      command.Parameters.Add(new SqlParameter("@inrego", rego));
      command.Parameters.Add(new SqlParameter("@inOprt", oprt));
      command.Parameters.Add(new SqlParameter("@inService", service));

      connection.Open();

      int update = command.ExecuteNonQuery();

      Console.WriteLine(update);

      connection.Close();
    }

更新在控制台显示 1,但数据库仍未更新。

这是存储过程

CREATE PROCEDURE [dbo].updateData
@inrego varchar(5),
@inOprt char(3),
@inService as varchar(50)

AS
delete from buses where rego = @inrego;
insert into buses (rego, operator,service) values(@inrego, @inOprt, @inService);
RETURN 0

手动运行存储过程,又名

USE [C:\USERS\---\DOCUMENTS\VISUAL STUDIO 2013\PROJECTS\---\TEST.DB.MDF]
GO

DECLARE @return_value Int

EXEC    @return_value = [dbo].[updateData]
        @inrego = N'1',
        @inOprt = N'2',
        @inService = N'3'

SELECT  @return_value as 'Return Value'

GO

工作,并成功更新数据库,但 C# 形式的代码没有。

【问题讨论】:

  • 我怀疑您可能只需要“dbo.updateData”来进行通话。
  • 刚刚尝试过@EugenePodskal。不幸的是,结果相同。
  • @GordonLinoff 修改它以在开始时添加 dbo,但仍然无法正常工作
  • @matthew5025 。 . .可能是权限问题。

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


【解决方案1】:

我无法在干净的本地数据库(MS SQL EXPRESS 2013、Win 8.1、.NET 4.5)上重现该问题:

CREATE TABLE [dbo].buses
(
    [rego] varchar(5) NOT NULL PRIMARY KEY,
    [operator] char(3),
    [service] varchar(50)
)
static void UpdateOrInsertBuses(String rego, String oprt, String service)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "updateData";
            command.Parameters.Add(new SqlParameter("@inrego", rego));
            command.Parameters.Add(new SqlParameter("@inOprt", oprt));
            command.Parameters.Add(new SqlParameter("@inService", service));

            connection.Open();
            try
            {
                int update = command.ExecuteNonQuery();

                Console.WriteLine(update);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
            finally
            {
                connection.Close();
            }
        }
    }
}

// ...
// Add data
UpdateOrInsertBuses("11", "12", "13");
UpdateOrInsertBuses("21", "22", "23");

// Update added
UpdateOrInsertBuses("21", "22", "Changed for sure");

所以,这是一些与您当前的代码无关的问题。正如@ Gordon Linoff 所建议的那样,它要么是权限问题,要么是一些干预更新的触发器,或者数据库由于某些原因恢复或忽略任何更改。

【讨论】:

  • 我其实是“Microsoft SQL Server 数据库文件(SqlClient)”作为数据源,我会尝试使用SQL server。
  • 那么,这个问题只与数据库文件有关,而不是与托管文件有关?嗯,我稍后会尝试使用数据库文件 DB,只是为了检查。
猜你喜欢
  • 2015-11-11
  • 1970-01-01
  • 2020-05-30
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 2011-09-16
相关资源
最近更新 更多