如何从C#中的存储过程中获取结果

时间:2014-08-10 13:46:56

标签: c# sql stored-procedures

我对C#完全陌生。我创建了一个包含6条记录的小table。我想使用store procedure获取这些记录。另外,我有简单的两种形式。一个是Main Form,用户可以键入Student Id以获得click on the find button的结果。

GetStudentById

CREATE Procedure GetStudentById (@Id VARCHAR)
AS
BEGIN
    SELECT * FROM dbo.Students WHERE Id = @Id
END

我试图找到自己的解决方案。因为,我正在学习这个。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinApp
{
    public partial class WinForm : Form
    {
        public WinForm()
        {
            InitializeComponent();
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }// End CloseBtn_Click

        private string getConnectionString()
        {
            string conStr = ConfigurationManager.ConnectionStrings["WinApp.Properties.Settings.WinPro_dbConnectionString"].ToString();
            return conStr;
        }

        private void FindBtn_Click(object sender, EventArgs e)
        {
            string SearchId = SearchInput.Text;

            SqlCommand cmd = null;
            using (SqlConnection con = new SqlConnection( getConnectionString() ))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.CommandText = "GetStudentById";
                    cmd.Parameters.AddWithValue("@Id", SearchId);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                    cmd = null;
                }
            }
        }// End FndBtn_Click
    }
}

我的主要表单上面的代码。如何将结果发送到array并分配给我的second viewform controls to edit the result

1 个答案:

答案 0 :(得分:1)

    SqlCommand cmd = null;
    using (SqlConnection con = new SqlConnection( getConnectionString() ))
    {
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "GetStudentById";
            cmd.Parameters.AddWithValue("@Id", SearchId);

            SQLDataReader reader = cmd.ExecuteReader();

            // To make your code a bit more robust you should get
            // the indexes of the named columns...this is so that if
            // you modified the Schema of your database (e.g. you
            // added a new column in the middle, then it is more
            // resilient than using fixed value index numbers).

            int iId = oReader.GetOrdinal("Id");
            int iFirstname = oReader.GetOrdinal("Firstname");
            int iLastname = oReader.GetOrdinal("Lastname"); 

            while(reader.Read())
            {
                int id = reader.GetInt32(iId);
                string firstname = reader.GetString(iFirstname);
                string lastname = reader.GetString(iLastname);

                ....
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            cmd = null;
        }
    }