在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)