Invalid object name error while trying to execute a stored procedure?

Not sure if I have a stored procedure named exactly what I am calling, but it always gives me this invalid object error. Here is the connection code, the error is thrown to the second to the last line.

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();
+3
source share
13 answers

Most likely, the stored procedure will not be visible to your code because it was created / owned by a user other than dboor the user specified in your connection string.

Have you tried the prefix for the name of the stored procedure dbo? For example, "dbo.SetMapping".

Management Studio/Enterprise Manager, , , dbo, /, .

+5

, , [master] , . , , , .

+2

sql- -?

? mgt studio qry?

+1

() .

, , , ( ).

+1

, , "dbo", "dbo.SetMapping", - "SomeUser.SetMapping"

"dbo".

sqlcmd.CommandText = "dbo.SetMapping";
+1

, ?

0

, .

script , script. , , , .

script db, , , script. ( 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

( , db, 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
0

? , # , ?

0

, ? sproc per se, - , , ( sproc ), , sproc, .

sproc , - sproc SQL Management Studio.

, , - . , , , sproc Sql ..

, .

0

, .NET 4, , .

DBML. , , , .

sproc designer.cs:

[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));
}

. .

UPDATE: , -, , Visual Studio , , ​​ dbml. .

"IsComposable", -, . node dbml, Visual Studio .

0

, .net. , ...

, , , , ""

, SQL ERROR , . , , ; ( , ) , . , , !

0

, .

. test2 test. :

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

0

. . select * From [ ]. [dbo]. [ ] in * []. [dbo]. [employee]

0

Source: https://habr.com/ru/post/1708424/


All Articles