簡體   English   中英

嘗試執行存儲過程時無效的對象名稱錯誤?

[英]Invalid object name error when trying to execute stored procedure?

不確定是什么交易我的存儲過程命名正是我所調用的,但它總是給我這個無效的對象錯誤。 這是連接代碼,錯誤是在那里的倒數第二行引發的。

SqlConnection cnstr = new SqlConnection(ConfigurationManager.ConnectionStrings["darconn"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand();

sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Connection = cnstr;
sqlcmd.CommandText = "SetMapping";

String[] pullKodID = bundlelist.SelectedValue.ToString().Split(':');
int kod_id = System.Convert.ToInt32(pullKodID[0]);

sqlcmd.Parameters.Add("@kod_id", kod_id);
sqlcmd.Parameters.Add("@ell_id", courselist.Items[i].Text);
cnstr.Open();
sqlcmd.ExecuteNonQuery();
cnstr.Close();

很可能存儲過程對您的代碼不可見,因為它是由dbo以外的用戶或連接字符串中指定的用戶創建/擁有的。

您是否嘗試使用dbo為存儲過程名稱添加前綴? 例如“dbo.SetMapping”。

如果做不到這一點,請使用Management Studio / Enterprise Manager查找誰擁有存儲過程,並將其重新創建為dbo或更新您的代碼/連接字符串以使用相應的用戶。

我遇到了同樣的問題,事實證明我在[master]數據庫中創建了存儲過程,而不是我應該處理的那個。 可能不是你的問題,但需要注意的是。

sql profiler顯示什么?

你可以在應用程序的上下文之外執行嗎? 來自mgt studio還是qry analyzer?

檢查存儲過程中表名的拼寫。

保存存儲過程時,它會檢查所用表中的fiel的名稱,但是可以保存使用不存在(或拼寫錯誤)的表名的存儲過程。

檢查存儲過程以查看它是否屬於'dbo',因此名稱將為'dbo.SetMapping'而不是'SomeUser.SetMapping'

我還會明確指定'dbo'。 在名字里

sqlcmd.CommandText = "dbo.SetMapping";

首先確保您的存儲過程實際上在.net之外工作。 如果工作正常,請考慮以下內容......

如果引用的對象工作正常,則此錯誤可能會產生誤導,請檢查您在同一頁面上使用的其他存儲過程,以便對“對象”進行無效引用

原因是它可能在您在同一頁面上運行的完全獨立的損壞存儲過程中引用SQL錯誤。 例如,假設您有一個存儲過程來獲取表的內容; with the (rather than the table the stored procedure was referencing) in another stored procedure on the same page then this error will be thrown. 如果您錯誤地將 (而不是存儲過程所引用的表)的表連接在同一頁面上的另一個存儲過程中,則會拋出此錯誤。 這可能會令人困惑,因為您知道引用的存儲過程工作得很好!

我在程序中有錯誤的表名,這就是我收到錯誤的原因。

示例:過程中有一個名為test2的表,其中包含表test字段。 它看起來像:

insert into test2 (fields from test) values ...

我有類似的問題。 我忘記寫完我的實際數據庫的完整路徑。 所以你必須寫select * From [你的數據庫名]。[dbo]。[對象名]在我的情況下是實際命名數據庫中的一個表對象所以,從[實用]中選擇*。[dbo]。[employee]同樣的邏輯也適用於存儲過程

它抱怨的實際對象的名稱是什么? 我在存儲過程中看到了同樣的問題,實際上sproc本身沒有問題 - 但是由於輸入錯誤,存儲過程中使用的表的名稱是錯誤的(sproc名稱被意外地粘貼在了表名)因此它返回了與sproc名稱匹配的無效對象名稱錯誤 - 這非常令人困惑。

嘗試更改sproc名稱並從代碼中再次調用它,看看會發生什么 - 或者嘗試直接在SQL Management Studio中運行sproc。

如果這不起作用那么非常有條理,分而治之 - 直接回到基礎並再次檢查端到端的一切。 連接字符串是否正確,它在運行時連接到什么數據庫,什么用戶ID,sproc在Sql管理工作室中自己運行等等

希望有所幫助。

每次我嘗試使用ADO.Net在SQL表中插入某些數據時,我也遇到了同樣的錯誤。 后來當我嘗試在SQL Server中直接執行相同的命令時,我在表上發現了一個阻止它的觸發器。 所以檢查一下。

對於它的價值,我在.NET 4上遇到了一個完全不同的問題,產生了相同的異常,幾乎沒有什么有用的信息來診斷它。

我在DBML中更新了幾個表。 我可能已經重新加載了一個存儲過程,但我不記得這樣做了。

我在designer.cs中留下了以下sproc包裝器:

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetSomeInformation", IsComposable=true)]
public IQueryable<GetSomeInformationResult> GetSomeInformation([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="BigInt")] System.Nullable<long> infoId)
{
    return this.CreateMethodCallQuery<GetSomeInformationResult>(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), infoId);
}

CreateMethodCallQuery用於調用表值函數,而不是存儲過程。 我很困惑為什么它會改變這一點。 我還原它使用ExecuteMethodCall

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetSomeInformation")]
public ISingleResult<GetSomeInformationResult> GetSomeInformation([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="BigInt")] System.Nullable<long> infoId)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), infoId);
    return ((ISingleResult<GetSomeInformationResult>)(result.ReturnValue));
}

一切都很好。 很奇怪。

更新:這似乎是由Visual Studio決定存儲過程在添加到dbml時是一個表值函數引起的。 我以前從未見過這種情況。

“IsComposable”標志似乎是區分存儲過程和表值函數的唯一方法。 一旦我從dbml中的Function節點清除了標志,Visual Studio就會在設計器文件中生成正確的代碼。

你絕對確定你輸入的名字是否正確?

這是一個很長的鏡頭,但已經列出了常見的答案。

很少,腳本可以在腳本中的目錄中獲得不同的名稱。 我相信這可能會導致類似於您所看到的問題。

以下腳本將檢查您的數據庫,以查看目錄中是否有與該腳本不匹配的項目。 (你需要SQL 2005或更高版本才能工作)

-------------------------------------------------------------------------
-- Check Syntax of Database Objects
-- Copyrighted (2009).  Free to use as a tool to check your own code or in 
--  any software not sold. All other uses require written permission from Author
-- Author: Stephen Schaff
-------------------------------------------------------------------------
-- Turn on ParseOnly so that we don't actually execute anything.
SET PARSEONLY ON 
GO

-- Create a table to iterate through
declare @ObjectList table (ID_NUM int NOT NULL IDENTITY (1, 1), OBJ_NAME varchar(255), OBJ_TYPE char(2))

-- Get a list of most of the scriptable objects in the DB.
insert into @ObjectList (OBJ_NAME, OBJ_TYPE)
SELECT   name, type
FROM     sysobjects WHERE type in ('P', 'FN', 'IF', 'TF', 'TR', 'V')
order by type, name

-- Var to hold the SQL that we will be syntax checking
declare @SQLToCheckSyntaxFor varchar(max)
-- Var to hold the name of the object we are currently checking
declare @ObjectName varchar(255)
-- Var to hold the type of the object we are currently checking
declare @ObjectType char(2)
-- Var to indicate our current location in iterating through the list of objects
declare @IDNum int
-- Var to indicate the max number of objects we need to iterate through
declare @MaxIDNum int
-- Set the inital value and max value
select  @IDNum = Min(ID_NUM), @MaxIDNum = Max(ID_NUM)
from    @ObjectList

-- Begin iteration
while @IDNum <= @MaxIDNum
begin
  -- Load per iteration values here
  select  @ObjectName = OBJ_NAME, @ObjectType = OBJ_TYPE
  from    @ObjectList
  where   ID_NUM = @IDNum 

  -- Get the text of the db Object (ie create script for the sproc)
  SELECT @SQLToCheckSyntaxFor = OBJECT_DEFINITION(OBJECT_ID(@ObjectName, @ObjectType))

  begin try
    -- Run the create script (remember that PARSEONLY has been turned on)
    EXECUTE(@SQLToCheckSyntaxFor)
  end try
  begin catch
    -- See if the object name is the same in the script and the catalog (kind of a special error)
    if (ERROR_PROCEDURE() <> @ObjectName)
    begin
      print 'Error in ' + @ObjectName
      print '  The Name in the script is ' + ERROR_PROCEDURE()+ '. (They don''t match)'
    end

  end catch

  -- Setup to iterate to the next item in the table
  select  @IDNum = case
            when Min(ID_NUM) is NULL then @IDNum + 1
            else Min(ID_NUM)
          end  
  from    @ObjectList
  where   ID_NUM > @IDNum

end
-- Turn the ParseOnly back off.
SET PARSEONLY OFF 
GO

(另請注意,如果要查看數據庫中的所有錯誤,請在if (ERROR_PROCEDURE() <> @ObjectName)塊之后添加此錯誤。)

else if (ERROR_MESSAGE() <> 'There is already an object named ''' + ERROR_PROCEDURE() + ''' in the database.')
begin
  -- Report the error that we got.
  print 'Error in ' + ERROR_PROCEDURE()
  print '  ERROR TEXT: ' + ERROR_MESSAGE() 
end

數據庫是否使用區分大小寫的排序規則? 如果是這樣,您的C#代碼中的情況與數據庫中的情況相同嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM