.net core调用SqlClient实现基础数据交互

1 先添加一个.net core项目

2 添加nuget包

System.data.dll中 system.data命名空间 提供不同的ado.net类

这里我们连接的是sql server数据库

    public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
            //Pooling=true 启用程序池 性能更快
            string constr = "Data Source=163.53.168.212;Initial Catalog=Inroad;Persist Security Info=True;User ID=sa;Password=Lnl244h;Pooling=true;Max Pool Size=200;Min Pool Size=5;MultipleActiveResultSets=true";

            //手动释放

            //SqlConnection connection = new SqlConnection(constr);
            //connection.Open();//打开连接
            //connection.Close();//关闭连接(连接字符串还在)
            //connection.Dispose();//释放连接(连接字符串不在)


            //自动释放
            using (SqlConnection connection = new SqlConnection(constr))
            {
                #region SqlCommand原始写法    ExecuteScalar
                string sqlselect = "select 1";
                //SqlCommand对sqlserver数据库执行sql语句或者存储过程,执行数据库命令的对象
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = sqlselect;
                //cmd.CommandType = CommandType.Text;//默认sql语句 可以不写
                //cmd.CommandType = CommandType.StoredProcedure;//sql为存储过程时
                #region 执行方式 查
                //增删改 返回受影响的行数
                connection.Open();
                var data = cmd.ExecuteScalar();//返回结果用object接收
                connection.Close();
                #endregion
                #endregion

                #region 精简写法   ExecuteNonQuery
                //string cmdText, SqlConnection connection, SqlTransaction transaction
                string sqlupdate = "update account set username='张三' where id='1'";
                SqlCommand cmd1 = new SqlCommand(sqlupdate, connection);
                #region 执行方式 增删改
                //增删改 返回受影响的行数
                connection.Open();
                int count = cmd1.ExecuteNonQuery();
                connection.Close();
                #endregion
                #endregion


                #region 精简写法   ExecuteReader
                //string cmdText, SqlConnection connection, SqlTransaction transaction
                string sqlselectclass = "select id,username,sex from account where id='1'";
                SqlCommand cmd2 = new SqlCommand(sqlselectclass);
                cmd2.Connection = connection;
                #region 执行方式 查询一个对象 用指针的形式读数据源
                //增删改 返回受影响的行数
                connection.Open();
                var dr = cmd2.ExecuteReader();
                //读取数据源 要即时保存 读一条 丢一条
                while (dr.Read())
                {
                    string id = dr["id"].ToString();
                    string username = dr["username"].ToString();
                    string sex = dr["sex"].ToString();
                }
                dr.Close();//需要即时关闭 占用内存大
                connection.Close();
                #endregion
                #endregion



            }
        }

posted @ 2022-08-24 14:08  原往  阅读(449)  评论(0编辑  收藏  举报