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.