tnblog
首页
视频
资源
登录

ado.net访问数据库的基本用法

2981人阅读 2019/2/26 17:50 总访问:334544 评论:0 收藏:0 手机
分类: NET


放回datatable

  //1:开打数据库的连接
            using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=test"))
            {
                conn.Open();
                //2:构建执行sql语句的对象,指定一个sql语句
                using (SqlCommand com = new SqlCommand("select * from UserInfo", conn))
                {
                    //3:执行sql填充到DataTable
                    SqlDataAdapter adapter = new SqlDataAdapter(com);
                    DataTable table = new DataTable();
                    adapter.Fill(table);

                    return table;
                }
            }

返回sqldatareader

 //1:开打数据库的连接
            using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=test"))
            {
                conn.Open();
                //2:构建执行sql语句的对象,指定一个sql语句
                using (SqlCommand com = new SqlCommand("select * from UserInfo", conn))
                {
                    //3:执行sql返回SqlDataReader
                    SqlDataReader reader = com.ExecuteReader();
                    //读取(SqlDataReader一行一行的读取)
                    while (reader.Read())
                    {
                        string userAcount = reader[1].ToString();
                        string username = reader["username"].ToString();
                        string userSex = reader["userSex"].ToString();
                    }
                }
            }


评价
吃亏决不亏,惜福才有福
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术