• ADO


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace ConsoleApp2
    {
        public static class SqlHelper
        {
            private static readonly string constr = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
    
            public static int ExecuteNonQuery(string sqlStr, CommandType commandType, params SqlParameter[] pms)
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(sqlStr, con))
                    {
                        cmd.CommandType = commandType;
                        if (pms != null)
                            cmd.Parameters.AddRange(pms);
    
                        con.Open();
                        object a= cmd.ExecuteNonQuery();
                        return 0;
                    }
                }
            }
    
            public static object ExecuteScalar(string sqlStr, CommandType commandType, params SqlParameter[] pms)
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(sqlStr, con))
                    {
                        cmd.CommandType = commandType;
                        if (pms != null)
                        {
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddRange(pms);
                        }
                        con.Open();
                        return cmd.ExecuteScalar();
                    }
                }
    
            }
    
    
            public static SqlDataReader ExecuteReader(string sqlStr, CommandType commandType, params SqlParameter[] pms)
            {
                SqlConnection con = new SqlConnection(constr);
                using (SqlCommand cmd = new SqlCommand(sqlStr, con))
                {
                    cmd.CommandType = commandType;
                    if (pms != null)
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddRange(pms);
                    }
                    try
                    {
                        if (con.State == ConnectionState.Closed)
                            con.Open();
                        return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    }
                    catch (Exception)
                    {
                        if (con.State == ConnectionState.Open)
                        {
                            con.Close();
                            con.Dispose();
                        }
                        throw;
                    }
                }
            }
    
            public static DataTable ExecuteTable(string sqlStr, CommandType commandType, params SqlParameter[] pms)
            {
                DataTable table = new DataTable();
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlDataAdapter adapt = new SqlDataAdapter(sqlStr, con))
                    {
                        adapt.SelectCommand.CommandType = commandType;
                        if (pms != null)
                        {
                            adapt.SelectCommand.Parameters.Clear();
                            adapt.SelectCommand.Parameters.AddRange(pms);
                        }
                        con.Open();
                        adapt.Fill(table);
                    }
                }
                return table;
            }
    
    
        }
    }
      private static void Insert()
            {
                List<string> list = new List<string>();
    
                List<SqlParameter> pms = new List<SqlParameter>();
                for (int i = 101; i < 1101; i++)
                {
                    string name = "name" + i;
                    string pwd = "pwd" + i;
                    string str = $"('@{name}','@{pwd}')";
                    pms.Add(new SqlParameter($"@{name}",$"@{pwd}"));
                    list.Add(str);
                }
                string sqlStr = $"insert into LoginData values {string.Join(",", list)}";
                int ii = SqlHelper.ExecuteNonQuery(sqlStr, System.Data.CommandType.Text,pms.ToArray());
                Console.WriteLine(ii);
            }
    
            private void Select()
            {
                Console.WriteLine("name:");
                string name = Console.ReadLine();
                Console.WriteLine("密码:");
                string pwd = Console.ReadLine();
    
                string sqlStr = "select count(0) from LoginData where name=@name and password=@pwd";
                List<SqlParameter> pms = new List<SqlParameter>()
                {
                    new SqlParameter("@name",name),
                    new SqlParameter("@pwd",pwd)
                };
                object num = SqlHelper.ExecuteScalar(sqlStr, CommandType.Text, pms.ToArray());
                Console.WriteLine(num);
            }
  • 相关阅读:
    LIST组件使用总结
    openlayers之interaction(地图交互功能)
    vbind:class绑定样式,决定样式的显示与否
    cesium之measure功能实现
    Cesium渲染效果差,锯齿明显,解决办法
    CSS让DIV层叠 两个DIV或多个DIV顺序重叠加
    ES6之import/export命令
    vantui:
    Openlayers简单要素的添加
    Vue中的this表示?
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/8955297.html
Copyright © 2020-2023  润新知