ADO.NET基本知识

1.连接数据库

      (1)在web.config文件中添加连接字符串

             < configuration >
                   < connectionStrings>
                         < add name=" 连接字符串" connectionString="Data Source=.;Initial Catalog=数据库;uid=sa;pwd=;"   providerName="System.Data.SqlClient" />
                  </ connectionString>
             </ configration >
 
        (2)在使用页面创建连接字符串
               SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[" 连接字符串"].ConnectionString);
 
2.访问和操作数据库(SqlCommand对象) 
      
         (1)普通SQL访问和操作
            conn.Open(); //打开数据库连接,conn为数据库连接字符串对象,同1中的conn
            SqlCommand cmd=new SqlCommand("delete from employees where eID="+eid, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
        
         (2)执行存储过程
             conn.Open();
            SqlCommand cmd = new SqlCommand(proc, conn);  //执行存储过程, proc为存储过程名
            cmd.CommandType = CommandType.StoredProcedure;//cmd命令类别
            cmd.Parameters.Add("@eid", SqlDbType.Int).Value = u.eID; //为存储过程 添加参数
            cmd.Parameters.Add("@ename", SqlDbType.VarChar, 10).Value = u.eName;
            
            cmd.ExecuteNonQuery(); // 执行cmd命令
 
3.绑定数据到控件
 
      以前用的比较多的数据绑定方法有两种
          <%# DataBinder.Eval(Container.DataItem,"字段名")%> 或者
          <%# DataBinder.Eval(Container,"DataItem.字段名")%>
    现在有一种叫做标准数据绑定的方法,而且微软也说这种方法的效率要比以上两种高。
         <%# ((DataRowView)Container.DataItem)["字段名"]%>           
         但记住要这样用必须要在前台页面导入名称空间System.Data,否则会生成错误信息。
         <%@ Import namespace="System.Data" %>
        这种用法其实和<%# ((DictionaryEntry)Container.DataItem).Key%>是一个道理。
 
            DataBinder是System.Web里面的一个静态类,它提供了Eval方法用于简化数据绑定表达式的编写,但是它
      使用的方式是通过Reflection等开销比较大的方法来达到易用性,因此其性能并不是最好的。而Container则
      根本不是任何一个静态的对象或方法,它是ASP.NET页面编译器在数据绑定事件处理程序内部声明的局部变
      量,其类型是可以进行数据绑定的控件的数据容器类型(如在Repeater内部的数据绑定容 RepeaterItem),在
      这些容器类中基本都有DataItem属性,因此你可以写Container.DataItem,这个属性返回 的是你正在被绑定的
      数据源中的那个数据项。如果你的数据源是DataTable,则这个数据项的类型实际是DataRowView。
       绑定到DataView,DataTable,DataSet:
        <%#((DataRowView)Container.DataItem)["字段名"]%>或
        <%#((DataRowView)Container.DataItem).Rows[0]["字段名"]%>
        要格式化则:
        <%#string.Format("{0:c}",((DataRowView)Container.DataItem)["字段名"])%>
        <%#DataBinder.Eval(Container.DataItem,"字段名","格式")%>

        绑定到DataReader:
        <%#((IDataReader)Container.DataItem).字段名%>

         绑定到DataSet、DataTable时:
       <%#((System.Data.DataRowView)Container.DataItem)["字段名"]%>
       <%#((System.Data.DataRowView)Container.DataItem)[索引]%>

        绑定到DataReader时:
       <%#((System.Data.Common.DbDataRecord)Container.DataItem)[索引]%>
       <%#((System.Data.Common.DbDataRecord)Container.DataItem)["字段名"]%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值