Asp. NET connection sql2008 Database Implementation code _ practical skills

Source: Internet
Author: User

Use the SqlConnection object to connect sql2000 the above version and use the SqlCommand object to read the database.

SqlCommand class overview:

Used to execute SQL statements or stored procedures on a SQL database.

Namespaces: System.Data.SqlClient

Assembly: System.Data (in System.Data.dll)

Properties of the SqlCommand class

1.CommandText

Gets or sets the Transact-SQL statement or stored procedure to execute on the data source.

2. CommandType

Gets or sets a value that indicates how the CommandText property is interpreted, and CommandType defaults to CommandType.Text, which means that executing the SQL statement requires commandtype.storedprocedure when the stored procedure is invoked. 3.Connection

Gets or sets the SqlConnection used by an instance of SqlCommand.

4.CommandTimeOut

Gets or sets the wait time before terminating an attempt to execute a command and generating an error.

Methods of SqlCommand classes

1.ExecuteNonQuery: An operation that does not return a value through this command, such as Update,insert,delete SQL commands, simply returns the number of rows affected by the execution of the command to the table.
2.ExecuteScalar: Can be used to execute a select query, but returns a single value for query aggregation, such as using count (), sum (), and SQL instructions for functions such as.
3.ExecuteReader: This method returns a DataReader object, which is a collection of the contents of the query result.

The following SqlConnection connects Sql2008 and performs simple data manipulation code:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;

Using System.Web.UI.WebControls;
Using System.Data.SqlClient;
Using System.Data;

Using System.Configuration; Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) {//connection S
    QL database String sqlconn = "Data source=seebro-pc\\sqlexpress;initial catalog=supermarket;integrated security=true";
    SqlConnection myconnection = new SqlConnection (sqlconn);

    Myconnection.open ();
    Define SqlCommand class SqlCommand mycommand = new SqlCommand ();
    Mycommand.connection = myconnection;
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.CommandText = "Bytype";
    Stored procedure parameter SqlParameter parinput = MYCOMMAND.PARAMETERS.ADD ("@type", Sqldbtype.smallmoney);
    Parinput.direction = ParameterDirection.Input;

    Parinput.value = 2;

    SqlDataReader myreader = Mycommand.executereader (); Response.Write ("<Table Border=1 cellspaceing=0 cellpadding=2> ");
    Response.Write ("<tr bgcolor= #DAB4B >"); 
    for (int i = 0; i < Myreader.fieldcount i++) Response.Write ("<td>" + myreader.getname (i) + "</td>");

    Response.Write ("</tr>");
      while (Myreader.read ()) {Response.Write ("<tr>"); for (int i = 0; i < Myreader.fieldcount i++) Response.Write ("<td>" + myreader[i).
      ToString () + "</td>");
    Response.Write ("</tr>");

    } Response.Write ("</table>");
    Myreader.close ();
  Myconnection.close (); }
}

The

Changes to the code after executing the SQL directive to achieve the same effect.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;

Using System.Web.UI.WebControls;
Using System.Data.SqlClient;
Using System.Data;

Using System.Configuration; Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) {//connection S
    QL database String sqlconn = "Data source=seebro-pc\\sqlexpress;initial catalog=supermarket;integrated security=true";
    SqlConnection myconnection = new SqlConnection (sqlconn);

    Myconnection.open ();
    Define SqlCommand class SqlCommand mycommand = new SqlCommand ("SELECT * from product where product. Price = 2", myconnection);

    SqlDataReader myreader = Mycommand.executereader ();
    Response.Write ("<table border=1 cellspaceing=0 cellpadding=2>");
    Response.Write ("<tr bgcolor= #DAB4B >"); 
    for (int i = 0; i < Myreader.fieldcount i++) Response.Write ("<td>" + myreader.getname (i) + "</td>"); Response.Write ("</Tr> ");
      while (Myreader.read ()) {Response.Write ("<tr>"); for (int i = 0; i < Myreader.fieldcount i++) Response.Write ("<td>" + myreader[i).
      ToString () + "</td>");
    Response.Write ("</tr>");

    } Response.Write ("</table>");
    Myreader.close ();
  Myconnection.close (); }
}

Operation Effect:

Project code has been uploaded.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.