How to execute a stored procedure within C# program

前端 未结 13 1651
别跟我提以往
别跟我提以往 2020-11-22 00:10

I want to execute this stored procedure from a C# program.

I have written the following stored procedure in a SqlServer query window and saved it as stored1:

<
相关标签:
13条回答
  • 2020-11-22 00:29

    this is an example of a stored procedure that returns a value and it's execution in c#

    CREATE PROCEDURE [dbo].[InsertPerson]   
    -- Add the parameters for the stored procedure here  
    @FirstName nvarchar(50),@LastName nvarchar(50),  
    @PersonID int output  
    AS  
    BEGIN  
        insert [dbo].[Person](LastName,FirstName) Values(@LastName,@FirstName)  
    
        set @PersonID=SCOPE_IDENTITY()  
    END  
    Go  
    
    
    --------------
     // Using stored procedure in adapter to insert new rows and update the identity value.  
       static void InsertPersonInAdapter(String connectionString, String firstName, String lastName) {  
          String commandText = "dbo.InsertPerson";  
          using (SqlConnection conn = new SqlConnection(connectionString)) {  
             SqlDataAdapter mySchool = new SqlDataAdapter("Select PersonID,FirstName,LastName from [dbo].[Person]", conn);  
    
             mySchool.InsertCommand = new SqlCommand(commandText, conn);  
             mySchool.InsertCommand.CommandType = CommandType.StoredProcedure;  
    
             mySchool.InsertCommand.Parameters.Add(  
                 new SqlParameter("@FirstName", SqlDbType.NVarChar, 50, "FirstName"));  
             mySchool.InsertCommand.Parameters.Add(  
                 new SqlParameter("@LastName", SqlDbType.NVarChar, 50, "LastName"));  
    
             SqlParameter personId = mySchool.InsertCommand.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 0, "PersonID"));  
             personId.Direction = ParameterDirection.Output;  
    
             DataTable persons = new DataTable();  
             mySchool.Fill(persons);  
    
             DataRow newPerson = persons.NewRow();  
             newPerson["FirstName"] = firstName;  
             newPerson["LastName"] = lastName;  
             persons.Rows.Add(newPerson);  
    
             mySchool.Update(persons);  
             Console.WriteLine("Show all persons:");  
             ShowDataTable(persons, 14); 
    
    0 讨论(0)
  • 2020-11-22 00:30
    using (SqlConnection sqlConnection1 = new SqlConnection("Your Connection String")) {
    using (SqlCommand cmd = new SqlCommand()) {
      Int32 rowsAffected;
    
      cmd.CommandText = "StoredProcedureName";
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Connection = sqlConnection1;
    
      sqlConnection1.Open();
    
      rowsAffected = cmd.ExecuteNonQuery();
    
    }}
    
    0 讨论(0)
  • 2020-11-22 00:34

    You mean that your code is DDL? If so, MSSQL has no difference. Above examples well shows how to invoke this. Just ensure

    CommandType = CommandType.Text
    
    0 讨论(0)
  • 2020-11-22 00:35
    SqlConnection conn = null;
    SqlDataReader rdr  = null;
    conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
    conn.Open();
    
    // 1.  create a command object identifying
    //     the stored procedure
    SqlCommand cmd  = new SqlCommand("CustOrderHist", conn);
    
    // 2. set the command object so it knows
    //    to execute a stored procedure
    cmd.CommandType = CommandType.StoredProcedure;
    
    // 3. add parameter to command, which
    //    will be passed to the stored procedure
    cmd.Parameters.Add(new SqlParameter("@CustomerID", custId));
    
    // execute the command
    rdr = cmd.ExecuteReader();
    
    // iterate through results, printing each to console
    while (rdr.Read())
    {
        Console.WriteLine("Product: {0,-35} Total: {1,2}", rdr["ProductName"], rdr["Total"]);
    }
    
    0 讨论(0)
  • 2020-11-22 00:36

    Please check out Crane (I'm the author)

    https://www.nuget.org/packages/Crane/

    SqlServerAccess sqlAccess = new SqlServerAccess("your connection string");
    var result = sqlAccess.Command().ExecuteNonQuery("StoredProcedureName");
    

    Also has a bunch of other features you might like.

    0 讨论(0)
  • 2020-11-22 00:37

    No Dapper answer here. So I added one

    using Dapper;
    using System.Data.SqlClient;
    
    using (var cn = new SqlConnection(@"Server=(local);DataBase=master;Integrated Security=SSPI"))
        cn.Execute("dbo.test", commandType: CommandType.StoredProcedure);
    
    0 讨论(0)
提交回复
热议问题