正文

[asp.net]ADO.NET笔记(续)2006-09-15 18:08:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/bakers/18555.html

分享到:

实例一: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属性的用法类似.

阅读(5123) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册