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];

      

    }

}

原文地址:https://www.cnblogs.com/fslnet/p/2465574.html