//这里是SQLHelper类
using System.Data.SqlClient;
using System.Configuration;
using System;
using System.Text;
namespace 尝试封装
{
    class SQLHelper
    {
        public static int ExecuteNonQuery(string sql, params SqlParameter[] paramters)
        {
            String connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    foreach (SqlParameter parameter in paramters)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                    return cmd.ExecuteNonQuery();
                }
             
            }
        }
    }
}
//这里是通过SqlParameter插入数据
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 尝试封装
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SQLHelper.ExecuteNonQuery("Insert into T_Persons(Name,Age) values(@Name,@Age)", new SqlParameter("Name", "andy"), new SqlParameter("Age", 33));
        }
    }
}
插入数据后的数据库: