简体   繁体   中英

How to refactor an insert stored procedure

Just need help taking this big chunk and making it as efficient as possible, I just started learning C# about 2 days ago so still very new to it. But I am writing code that uses an insert stored procedure from my SQL database.

Please also explain why your changes are efficient as well. I just started learning C# so I haven't written anything to connect to the front end of anything, I just thought it'd be cool to be able to write my inputs into the console app and still be able to use a stored procedure. Also one issue that I am facing is that, everything works until i get to the skills input, and it will just skip that part entirely.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;


namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection PubsConn = new SqlConnection("Server=.\\SQLEXPRESS;Database=PeopleDatabase;Trusted_Connection=True;"))
            {


                SqlCommand testCMD = new SqlCommand("People_Insert", PubsConn);

                testCMD.CommandType = CommandType.StoredProcedure;

                SqlParameter Id = testCMD.Parameters.Add("@Id", SqlDbType.Int, 5);
                Id.Direction = ParameterDirection.Output;
                SqlParameter Title = testCMD.Parameters.Add("@Title", SqlDbType.NVarChar, 100);
                Title.Direction = ParameterDirection.Input;
                SqlParameter Bio = testCMD.Parameters.Add("@Bio", SqlDbType.NVarChar, 100);
                Bio.Direction = ParameterDirection.Input;
                SqlParameter Summary = testCMD.Parameters.Add("@Summary", SqlDbType.NVarChar, 100);
                Summary.Direction = ParameterDirection.Input;
                SqlParameter Headline = testCMD.Parameters.Add("@Headline", SqlDbType.NVarChar, 100);
                Headline.Direction = ParameterDirection.Input;
                SqlParameter Slug = testCMD.Parameters.Add("@Slug", SqlDbType.NVarChar, 100);
                Slug.Direction = ParameterDirection.Input;
                SqlParameter StatusId = testCMD.Parameters.Add("@StatusId", SqlDbType.Int, 3);
                StatusId.Direction = ParameterDirection.Input;
                SqlParameter Skills = testCMD.Parameters.Add("@Skills", SqlDbType.NVarChar, 100);
                Skills.Direction = ParameterDirection.Input;
                SqlParameter PrimaryImage = testCMD.Parameters.Add("@PrimaryImage", SqlDbType.NVarChar, 100);
                PrimaryImage.Direction = ParameterDirection.Input;

                string titleInput;
                string bioInput;
                string summaryInput;
                string headlineInput;
                string slugInput;
                int statusId;
                string skillsInput;
                string imageInput;
                string strRowAffect;

                Console.WriteLine("Please Enter a Title");
                titleInput = Console.ReadLine();
                Title.Value = titleInput;

                Console.WriteLine("Please Enter a Bio");
                bioInput = Console.ReadLine();
                Bio.Value = bioInput;

                Console.WriteLine("Please Enter a Summary");
                summaryInput = Console.ReadLine();
                Summary.Value = summaryInput;

                Console.WriteLine("Please Enter a Headline");
                headlineInput = Console.ReadLine();
                Headline.Value = headlineInput;

                Console.WriteLine("Please Enter a Unique Slug");
                slugInput = Console.ReadLine();
                Slug.Value = slugInput;

                Console.WriteLine("Please Enter a 1 digit Status Id");
                statusId = Console.Read();
                StatusId.Value = statusId;

                Console.WriteLine("Please Enter Skills");
                skillsInput = Console.ReadLine();
                Skills.Value = skillsInput;

                Console.WriteLine("Enter an Image Url");
                imageInput = Console.ReadLine();
                PrimaryImage.Value = imageInput;

                PubsConn.Open();

                strRowAffect = testCMD.ExecuteNonQuery().ToString();

                Console.WriteLine("Number of Rows: " + strRowAffect);
                Console.WriteLine("Return Value: " + Id.Value);

            }

            Console.ReadLine();
        }
    }
}

Your code looks fine. There is no efficiency boost in my example, it's just a visual refactoring. Parameters have Input direction by default, so you don't need to set them explicitly.

static void Main(string[] args)
{
    var command = new SqlCommand("People_Insert");
    command.CommandType = CommandType.StoredProcedure;

    var idParam = command.Parameters.Add("@Id", SqlDbType.Int, 5);
    idParam.Direction = ParameterDirection.Output;

    var titleParam = command.Parameters.Add("@Title", SqlDbType.NVarChar, 100);
    var bioParam = command.Parameters.Add("@Bio", SqlDbType.NVarChar, 100);
    var summaryParam = command.Parameters.Add("@Summary", SqlDbType.NVarChar, 100);
    var headlineParam = command.Parameters.Add("@Headline", SqlDbType.NVarChar, 100);
    var slugParam = command.Parameters.Add("@Slug", SqlDbType.NVarChar, 100);
    var statusIdParam = command.Parameters.Add("@StatusId", SqlDbType.Int, 3);
    var skillsParam = command.Parameters.Add("@Skills", SqlDbType.NVarChar, 100);
    var primaryImageParam = command.Parameters.Add("@PrimaryImage", SqlDbType.NVarChar, 100);

    Console.WriteLine("Please Enter a Title");
    titleParam.Value = Console.ReadLine();

    Console.WriteLine("Please Enter a Bio");
    bioParam.Value = Console.ReadLine();

    Console.WriteLine("Please Enter a Summary");
    summaryParam.Value = Console.ReadLine();

    Console.WriteLine("Please Enter a Headline");
    headlineParam.Value = Console.ReadLine();

    Console.WriteLine("Please Enter a Unique Slug");
    slugParam.Value = Console.ReadLine();

    Console.WriteLine("Please Enter a 1 digit Status Id");
    statusIdParam.Value = Console.Read();

    Console.WriteLine("Please Enter Skills");
    skillsParam.Value = Console.ReadLine();

    Console.WriteLine("Enter an Image Url");
    primaryImageParam.Value = Console.ReadLine();

    using (var connection = new SqlConnection("Server=.\\SQLEXPRESS;Database=PeopleDatabase;Trusted_Connection=True;"))
    {
        connection.Open();
        command.Connection = connection;
        var affectedRowsCount = command.ExecuteNonQuery();

        Console.WriteLine("Number of Rows: " + affectedRowsCount);
        Console.WriteLine("Return Value: " + idParam.Value);
    }

    Console.ReadLine();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM