Home > Backend Development > C++ > How Can I Execute a SQL Server Stored Procedure in C# and Resolve the 'Cannot find the stored procedure' Error?

How Can I Execute a SQL Server Stored Procedure in C# and Resolve the 'Cannot find the stored procedure' Error?

DDD
Release: 2025-01-30 17:36:11
Original
227 people have browsed it

How Can I Execute a SQL Server Stored Procedure in C# and Resolve the

execute SQL Server storage procedures in C#

This article will guide you how to successfully perform SQL Server storage procedures in the C#program and solve the common "unable to find storage procedure" error.

Assume that your C#program attempts to execute the SQL Server database storage procedure named "DBO.TEST", but encounters an abnormal "unable to find the storage procedure dbo.test". This error indicates that the database cannot identify the stored procedure.

To perform the storage procedure from C#, please follow the steps below to operate:

    Establish a database connection:
  1. Use the correct connection string to create a

    object and open the connection. SqlConnection

    Create SQLCOMMAND object:
  2. Use the storage procedure name as
  3. Attribute to initialize a

    object. Set the attribute to to indicate that you are performing the storage procedure. CommandText SqlCommand CommandType Execute the storage procedure: CommandType.StoredProcedure Use the

    method of the
  4. object to perform the storage procedure, and this method will not return any data. Alternatively, you can use the method to retrieve data from the storage procedure.
  5. The following is a modified code example: SqlCommand ExecuteNonQuery ExecuteReader Important Tips:

Please replace to your own database connection string.

<code class="language-csharp">using System;
using System.Data;
using System.Data.SqlClient;

namespace AutomationApp
{
    class Program
    {
        public void RunStoredProc()
        {
            SqlConnection conn = null;

            Console.WriteLine("\n正在执行存储过程...\n");

            try
            {
                conn = new SqlConnection("Server=(local);DataBase=master;Integrated Security=SSPI"); // 请替换为您的连接字符串
                conn.Open();
                SqlCommand cmd = new SqlCommand("dbo.test", conn); // 请确保存储过程名称正确
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();
                Console.WriteLine("存储过程执行成功!");
            }
            catch (SqlException ex)
            {
                Console.WriteLine($"存储过程执行失败:{ex.Message}");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.RunStoredProc();      
            Console.ReadKey();
        }
    }
}</code>
Copy after login
Carefully check the name of the storage procedure

Whether it is accurate. Smallwriter sensitive! Add error treatment (TRY-CATCH) to better handle potential database errors.

  • Please confirm the storage procedure that has been correctly created in the database. The storage procedure is located in the database. Through its unique name logo, no path is required. "Server=(local);DataBase=master;Integrated Security=SSPI"

The above is the detailed content of How Can I Execute a SQL Server Stored Procedure in C# and Resolve the 'Cannot find the stored procedure' Error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template