sqlcommand對象的有幾個操作方法

來源:互聯網
上載者:User

    1、sqlcommand.ExecuteNonQuery ()該方法主要是執行SQL語句的插入、修改、刪除的命令、返回所影響的行數,並不返回操作資料庫中資料表的資料。

   2、sqlcommand.ExecuteReader()該方法主要是執行SQL的select語句,ExecuteReader()方法主要提供順序讀取資料庫中的資料的方法,然後返回SqlReader對象,編者可以使用read的方法迴圈依次讀取每個記錄中各欄位的內容。若要建立SqlDataReader,必須調用sqlcommand對象的ExecuteReader()方法來返回資料庫中資料表的資料:

      SqlConnection conn = new SqlConnection("  ");
        string sqlcmd="select * from 表名字";
        SqlCommand cmd =new SqlCommand (sqlcmd ,conn );
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
    if  (dr.Read ()==true )
        {
            ListBox1.Items.Add(string .Format ("[{0}],\"{1}\"", dr[0],dr [1]));
        }

    conn。close();

    3、sqlcommand.Excutescaler()此方法主要是用來返回一個值的情況,例如使用count()函數求表中的所有記錄的條數,或者是使用sum()求資料的函數求和。sqlcommand.commandtext是為了擷取或者設定來執行T——SQL語句、表名和預存程序的。

#region 運用 sqlcommand.Excutescaler()來返回資料的行數
        SqlConnection conn = new SqlConnection(" ");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        conn.Open();
        cmd .CommandText  = "select count(*) from表名";
        int num = (int)cmd.ExecuteScalar();
        Response.Write(string.Format("{0}", num));
#endregion

 

   

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.