zoukankan      html  css  js  c++  java
  • mssql数据集操作方法

    using System;

    using System.Data;

    using System.Data.SqlClient;

    using System.Collections.Generic;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            string constring = "data source=127.0.0.1;Database=;user id=;password=";

            SqlConnection myconn = new SqlConnection(constring);

            string sql = "";

            //SqlCommand mycmd = new SqlCommand(sql);

            // mycmd.Connection = myconn;

            SqlCommand mycmd = new SqlCommand(sql, myconn);

            myconn.Open();

            int rows = mycmd.ExecuteNonQuery();//返回受影响的行数,多用于 insert,update,delete,create

            myconn.Close();

            string sql2 = "";

            SqlCommand mycmd2 = new SqlCommand(sql2, myconn);

            myconn.Open();

            int count = (int)mycmd.ExecuteScalar();//返回单个值,返回记录集中第一行第一列,忽略额外的行和列

            myconn.Close();

            string sql3 = "";

            SqlCommand mycmd3 = new SqlCommand(sql3, myconn);

            myconn.Open();

            SqlDataReader dr = new SqlDataReader();//返回一行或多行,多用于select

            while (dr.Read())

            {

                //遍历结果集

                Response.Write(dr.GetInt32(0) + "  " + dr.GetString(1).ToString());

                //遍历结果集,使用序数索引器

                Response.Write(dr[0].ToString() + "  " + dr[1].ToString());

                //遍历结果集,使用列名索引器

                Response.Write(dr["id"].ToString() + "  " + dr["name"].ToString());

                //FieldCount(获取当前行中的列数【int】),HasRows(获取当前记录集中的是否包含一行或多行【bool】)

            }

            dr.Close();

            myconn.Close();

            string sql4 = "sql1;sql2";

            SqlCommand mycmd4 = new SqlCommand(sql4, myconn);

            myconn.Open();

            SqlDataReader drm = new SqlDataReader();//返回一行或多行,多用于select

            do

            {

                while (drm.Read())

                {

                    //遍历结果集

                    Response.Write(drm.GetInt32(0) + "  " + drm.GetString(1).ToString());

                    //遍历结果集,使用序数索引器

                    Response.Write(drm[0].ToString() + "  " + drm[1].ToString());

                    //遍历结果集,使用列名索引器

                    Response.Write(drm["id"].ToString() + "  " + drm["name"].ToString());

                }

            } while (drm.NextResult());

            drm.Close();

            myconn.Close();

            using (SqlConnection conn = new SqlConnection(constring)) 

            {

                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "sql6";

                conn.Open();

                using (SqlDataReader drx = cmd.ExecuteReader())//返回一行或多行,多用于select

                {

                    while (drx.Read())

                    {

                        //遍历结果集

                        Response.Write(drx.GetInt32(0) + "  " + drx.GetString(1).ToString());

                        //遍历结果集,使用序数索引器

                        Response.Write(drx[0].ToString() + "  " + drx[1].ToString());

                        //遍历结果集,使用列名索引器

                        Response.Write(drx["id"].ToString() + "  " + drx["name"].ToString());

                    }

                }

            }

          

        }

        public void getDataAdapter(){

            string constring = "data source=127.0.0.1;Database=;user id=;password=";

            SqlConnection myconn = new SqlConnection(constring);

            SqlDataAdapter adapter = new SqlDataAdapter("select * from userinfo ", myconn);

            //SqlCommand cmd = new SqlCommand("select * from userinfo where id>@id ",myconn);

            //cmd.Parameters.AddWithValue("@id",str);

            //SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            DataTable data = new DataTable();

            adapter.Fill(data);

            for (int i = 0; i < data.Rows.Count; i++)

            {

                Response.Write(data.Rows[i]["id"]+" "+data.Rows[i]["name"]);

            }

           // DataRow dr = data.Rows[n];

          

        }

    }

  • 相关阅读:
    竖版文字排列实现《金刚般若波罗蜜心经》
    前端气泡效果实现的方式
    纯CSS绘制三角形
    什么是块级格式上下文
    绝对定位元素left、right、top、bottom值与其margin和宽高的关系
    currentColor在CSS的含义
    HTML/css清除浮动的几种方式
    W3C中不同标准的含义
    table表格标签的属性
    输入你的生日某年某月某日,判断这一天是这一年的第几天、星期几?
  • 原文地址:https://www.cnblogs.com/fslnet/p/2465574.html
Copyright © 2011-2022 走看看