c#代码连接数据库,以及进行crud操作等

在C#的Web.config 里




<configuration>

之间加入下面代码:

<!--连接本地oracle数据库-->
  <location allowOverride="true">
    <appSettings>
      <add key="DbConnString" value="Data Source=(local);Database=MyData;User Id=sa;Password=123456;"/>
    </appSettings>
  </location>
  <!--结束-->

</configuration>


 


.cs代码文件里大致总结如下:

 //获取数据库字符串连接字符
    static String strSqlConn = System.Configuration.ConfigurationManager.AppSettings["DbConnString"];

   String sql = "select max(nrow) as maxrow from treenode";
        //using确保资源能被释放//连接数据库
        using ( SqlConnection conn = new SqlConnection(strSqlConn))
        {
            try
            {
                SqlCommand command = new SqlCommand(sql, conn);
                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        maxrow = reader.GetInt32(reader.GetOrdinal("maxrow"));

                    }
               
            }
            catch (SqlException ex)
            {
                throw ex;
            }

        }

--------------------------------------------------------------------------------------------------------------------------------------------------

下面是一个具体的.cs连接数据库进行操作的代码源文件

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Data;
/// <summary>
/// TreeDao 的摘要说明
/// </summary>
public class TreeDao
{
    //最大的行数
    static int maxrow;
    //获取数据库字符串连接字符
    static String strSqlConn = System.Configuration.ConfigurationManager.AppSettings["DbConnString"];
    //静态的存取删除树根节点的下面节点集合、
    static List<TreeNode> list1 = null;
    SqlConnection conns = null;
 
    /// <summary>
    /// 查询出最大行数
    /// </summary>
    public static int queryMax()
    {
       
        String sql = "select max(nrow) as maxrow from treenode";
        //using确保资源能被释放//连接数据库
        using ( SqlConnection conn = new SqlConnection(strSqlConn))
        {
            try
            {
                SqlCommand command = new SqlCommand(sql, conn);
                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        maxrow = reader.GetInt32(reader.GetOrdinal("maxrow"));

                    }
               
            }
            catch (SqlException ex)
            {
                throw ex;
            }

        }
        return maxrow;
    }
    /// <summary>
    /// 将查询出来的所有节点组装成树
    /// </summary>
    public static TreeNode assemble(List<TreeNode> list)
    {
        //树的最大行
        int c = queryMax();
        //将树的所有节点放入字典里(这里用数据字典就可以避免双重(多重)循环,提高查询效率,避免n次级增长)
        Dictionary<String, TreeNode> d = new Dictionary<string, TreeNode>();
        for (int i = 0; i < list.Count; i++)
        {
            d.Add(list[i].nid, list[i]);
        }
        //从树的最大行开始,直到根节点(寻找绑定儿子节点)
        while (c > 1)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值