Writing sqlhelper in visual

Time:2020-10-17
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Data;
 using System.Data.SqlClient;
 using System.Configuration;

insert reference

  public static string strconn = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;

Used to get the link string

public static DataTable Cha(string sql)
        {
            DataTable table = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(sql, strconn);
            sda.Fill(table);
            return table;
        }

Methods for querying data

public static int ZSG(string sql)
        {
            int num = 0;
            SqlConnection conn = new SqlConnection(strconn);
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            num = cmd.ExecuteNonQuery();
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            return num;
        }

It is used to add new data, delete data and modify data