实例一:ExecuteNonQuery用法--执行Command. SqlCommand Comm.=new SqlCommand(“select * from mp3 where id>260000”,conn); Comm. ExecuteNonQuery(). 实例二. 使用ExecuteReader方法. <% @ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { OleDbConnection Conn=new OleDbConnection(); Conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"+ "Data Source="+Server.MapPath("person.mdb"); Conn.Open(); OleDbCommand Comm=new OleDbCommand("select * from grade",Conn); OleDbDataReader dr=Comm.ExecuteReader(); dg.DataSource=dr; dg.DataBind(); Conn.Close(); } </script> <asp:DataGrid id="dg" runat="server" /> 实例三: 试用ExecuteScalar方法. Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored. Namespace: System.Data.SqlClientAssembly: System.Data (in system.data.dll) <% @ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { OleDbConnection Conn=new OleDbConnection(); Conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"+ "Data Source="+Server.MapPath("person.mdb"); Conn.Open(); String strSQL="select avg(数学) from grade"; OleDbCommand Comm=new OleDbCommand(strSQL,Conn); Double d=(Double)Comm.ExecuteScalar(); Message.Text="所有人数学的平均成绩为"+d.ToString()+"分"; Conn.Close(); } </script> <asp:Label id="Message" runat="server" /> 3 DataReader对象. Provides a way of reading a forward-only stream of rows from a SQL Server database. This class cannot be inherited. 取该对象的数据,有两种方法: 第一. 通过和DataGrid等数据控件绑定.直接输出. 第二. 另外是利用循环将其数据输出. 实例一. 试用DataReader对象输出数据. <% @ Page Language="C#" %> <%@Import Namespace="System.Data"%> <%@Import Namespace="System.Data.OleDb"%> <Script runat="server"> void Page_Load(Object Src, EventArgs E) { OleDbConnection Conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;"+ "Data Source="+Server.MapPath("person.mdb")); Conn.Open(); String strSQL="select * from grade"; OleDbCommand Comm=new OleDbCommand(strSQL,Conn); OleDbDataReader dr=Comm.ExecuteReader(); string html="<Table border=1>"; html+="<TR>"; html+="<TD><B>学号</B></TD>"; html+="<TD><B>姓名</B></TD>"; html+="<TD><B>数学</B></TD>"; html+="</TR>"; try{//读出每一条记录 while(dr.Read()) { html+="<TR>"; html+="<TD>" + dr["学号"].ToString() + "</TD>"; html+="<TD>" + dr["姓名"].ToString() + "</TD>"; html+="<TD>" + dr["数学"].ToString() + "</TD>"; html+="</TR>"; } html+="</Table>"; } finally{//关闭链接 dr.Close(); Conn.Close(); } Response.Write(html); } </Script> 实例二: 来自MSDN private static void ReadOrderData(string connectionString) { string queryString ="SELECT OrderID, CustomerID FROM dbo.Orders;"; using (SqlConnection connection =new SqlConnection(connectionString)) { SqlCommand command =new SqlCommand(queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1])); } reader.Close(); } } 注: 其中reader[0],reader[1]分别取出该条信息的第一,第二字段,相当于ASP中的rs的用法. 4.DataAdapter. Name Description SqlDataAdapter () Initializes a new instance of the SqlDataAdapter class. Supported by the .NET Compact Framework. SqlDataAdapter (SqlCommand) Initializes a new instance of the SqlDataAdapter class with the specified SqlCommand as the SelectCommand property. Supported by the .NET Compact Framework. SqlDataAdapter (String, SqlConnection) Initializes a new instance of the SqlDataAdapter class with a SelectCommand and a SqlConnection object. Supported by the .NET Compact Framework. SqlDataAdapter (String, String) Initializes a new instance of the SqlDataAdapter class with a SelectCommand and a connection string. Supported by the .NET Compact Framework. 实例一. Fill data into dataset <% @ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> protected void Page_Load(Object sender, EventArgs e) { OleDbConnection Conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;"+ "Data Source="+Server.MapPath("person.mdb")); string sql="select * from grade"; Conn.Open(); OleDbDataAdapter da = new OleDbDataAdapter(sql, Conn);//执行SQL DataSet ds = new DataSet(); da.Fill(ds, "grade"); dg.DataSource=ds.Tables["grade"].DefaultView; dg.DataBind(); } </script> <ASP:DataGrid id="dg" runat="server" /> 实例二. 试用DataAdapter的SelectCommand属性 <% @ Page Language="C#" %> <%@Import Namespace="System.Data"%> <%@Import Namespace="System.Data.OleDb"%> <Script language="C#" runat="server"> void Page_Load(Object Src, EventArgs E) { OleDbConnection Conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;"+ "Data Source="+Server.MapPath("person.mdb")); string strSQL="select * from grade"; OleDbCommand Comm=new OleDbCommand(strSQL,Conn); OleDbDataAdapter da=new OleDbDataAdapter(); da.SelectCommand=Comm; Conn.Open(); DataSet ds = new DataSet(); da.Fill(ds,"grade"); dg.DataSource=ds.Tables["grade"].DefaultView; dg.DataBind(); Conn.Close(); } </script> <ASP:DataGrid id="dg" runat="server"/> 总结: 同理 UpdateCommand, InsertCommand, DeleteCommand属性的用法类似.

评论