mssql資料集操作方法

來源:互聯網
上載者:User

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

      

 

    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.