ASP.NET连接数据库并获取数据

时间:2022-05-16 03:42:22

 *连接对象的用法SqlConnection,SqlCommand,SqlDataAdapter
 *数据访问方式的写法  

1.获取数据  :  

            //引用这两个命名空间

            using System.Data.SqlClient;
            using System.Data;

            // 初始化连接对象
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "User ID=sa;Initial Catalog=DataBaseName;Data Source= (local);Password=111111";

            // 打开连接
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }  

            // 初始化命令
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "sql语句";

            // 用于执行数据插入、更新和删除的操作;返回被影响的行数。
            int i = cmd.ExecuteNonQuery();  

             if(i>0){MessageBox.Show("操作成功");}
            // 用于查询最大值等只需返回一条数据情况下的操作;返回的是首行第一列的数据。                       

              object obj = cmd.ExecuteScalar();

            // 如果想获取数据集合的话我们经常使用到的是数据适配器
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            adapter.Fill(dt);  

2.把数据绑定到数据控件 

              string str = "Data Source=.;Initial Catalog=GridView;User ID=sa;Password=111111";        

              string sql = "select * from UserName";
            SqlConnection conn = new SqlConnection(str);
            //conn.Open(); 使用 SqlDataAdapter(数据适配器)不用写 

             //SqlCommand comm = new SqlCommand(sql, conn);
            //SqlDataAdapter dr = new SqlDataAdapter(comm); 

 

             SqlDataAdapter dr = new SqlDataAdapter(sql,conn);//上面两句可以合并成这一       

 

           DataSet ds = new DataSet();//创建数据集;

 

            dr.Fill(ds); //填充数据集

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();//讲数据源绑定到控件上,
            //conn.Close();   关闭数据库连接    
            if (conn.State==ConnectionState.Open) //判断数据库连接状态,是否连接
            {
                conn.Close();
            }

 

3.使用SqlDataReader:

     若要创建 SqlDataReader,必须调用 SqlCommand 对象的 ExecuteReader 方法,而不要直接使用构造函数。

        string str = "Data Source=.;Initial Catalog=GridView;User ID=sa;Password=111111";
            string sql = "select * from UserName";
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataReader dr = comm.ExecuteReader();
          
            if (dr.Read())
            {
                //下面两种都可以获得数据
                //this.TextBox1.Text = dr.GetString(1);
                //this.TextBox2.Text = dr.GetInt32(3).ToString();  

                this.TextBox1.Text = dr.GetString(dr.GetOrdinal("Name"));
                this.TextBox2.Text = dr.GetInt32(dr.GetOrdinal("Age")).ToString();
            }

            //循环输出

            while (dr.Read())
            {
                Response.Write(dr["Name"]);
                Response.Write(dr["Age"]);
                Response.Write("<br/>");
            }
            dr.Close();
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

SqlDataReader:提供一种从 SQL Server 数据库读取行的只进流的方式