Học Lập Trình

Cách Thêm dữ liệu từ c# lưu vào database sql server

Hướng dẫn Cách Thêm dữ liệu từ c# lưu vào database sql server bằng các thông thường và cách thêm có dùng parameter dùng ExecuteNonQuery

Thêm dữ liệu vào bảng trong SQL Server

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

Code hướng dẫn chương trình thêm sản phẩm vào database sql server bằng c#
namespace kncsdlhocba
{
    public partial class Themdulieu : Form
    {
        public Themdulieu()
        {
            InitializeComponent();
        }
        SqlConnection connec = null;
        string strcon = @”Server =DESKTOP-CR8PM8T\SQLEXPRESS; Database=CSDLSANPHAM ; User ID=sa; pwd =123″;
        private void Themdulieu_Load(object sender, EventArgs e)
        {
            HienThiLenlistview();
        }

        private void HienThiLenlistview()
        {
            if (connec == null)
            {
                connec = new SqlConnection(strcon);
            }
            if (connec.State == ConnectionState.Closed)
            {
                connec.Open();
            }

            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = “select * from SanPham”;
            command.Connection = connec;

            SqlDataReader reader = command.ExecuteReader();
            lsvSanPham.Items.Clear();
            while (reader.Read())
            {
                ListViewItem lvi = new ListViewItem(reader.GetInt32(0) + “”);
                lvi.SubItems.Add(reader.GetString(1));
                lvi.SubItems.Add(reader.GetInt32(2) + “”);
                lvi.SubItems.Add(reader.GetInt32(3) + “”);
                lsvSanPham.Items.Add(lvi);
            }
            reader.Close();
        }

 private void button1_Click(object sender, EventArgs e)// Không dùng paramater

        {
            if (connec==null)
            {
                connec = new SqlConnection(strcon);

            }
            if (connec.State==ConnectionState.Closed)
            {
                connec.Open();
            }
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.Text;
            string sql = “Insert Into SanPham (Ma,TenSanPham,DonGia,MaDM)”+
               ” values (“+txtMa.Text+” ,N’ “+txtTen.Text+”‘, “+txtGia.Text+”,”+txtMaDM.Text+”) “;
            command.CommandText = sql;
            command.Connection = connec;

           int ret =  command.ExecuteNonQuery();// Trả về giá trị dòng inset vào
            if (ret>0)//
            {
                HienThiLenlistview();
                MessageBox.Show(“Thêm Thành Công”);
            }
            else
            {
                MessageBox.Show(“Thêm Thất bại”);
            }
        }

private void btnpara_Click(object sender, EventArgs e) //Thêm có dùng paramater

        {
            if (connec==null)
            {
                connec = new SqlConnection(strcon);
            }
            if (connec.State==ConnectionState.Closed)
            {
                connec.Open();
            }
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = “Insert into  SanPham(Ma,TenSanPham,DonGia,MaDM) values(@ma, @ten, @gia,@mdm)”;
            command.Connection = connec;

            command.Parameters.Add(“@ma”, SqlDbType.Int).Value = txtMa.Text;
            command.Parameters.Add(“@ten”, SqlDbType.NVarChar).Value = txtTen.Text;
            command.Parameters.Add(“@gia”, SqlDbType.Int).Value = txtGia.Text;
            command.Parameters.Add(“@mdm”, SqlDbType.Int).Value = txtMaDM.Text;

            int ret = command.ExecuteNonQuery();
            if (ret>0)
            {
                HienThiLenlistview();
                MessageBox.Show(“Thêm Sản Phẩm”);
            }
        }
    }
}