连接SQL Server

web.config 配置

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<connectionStrings>
<add name="ConnectionString" connectionString="server= XXXX;database=XXX;uid=XXXX;pwd=XXXX" providerName="System.Data.SqlClient"></add>
</connectionStrings>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;                          //引用
using System.Data.SqlClient;                //引用

public partial class Nav : System.Web.UI.Page{
    SqlConnection cnn;                      //声明cnn    
    SqlCommand cmd;                         //指定在数据库上执行的操作的类型
    SqlDataReader datar;                    //记录集 
    string str_sql;
    protected void Page_Load(object sender, EventArgs e){
        makenav();                          //函数
    }

    protected void makenav(){
        string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;     //调用配置文件的配置
        cnn = new SqlConnection(conString);                 //实例化,连接数据库
        cnn.Open();                                         //打开数据库

        str_sql = "SELECT t_menuL1.*,t_menuL0.* FROM t_menuL1 RIGHT JOIN t_menuL0 ON t_menuL1.m1_m0id=t_menuL0.m0_id;"; //连接查询SQL语句
        cmd = new SqlCommand(str_sql,cnn);          
        datar = cmd.ExecuteReader();

        cnn.Close();
    }
        /*while (datar.Read())
        {
            div_menu.InnerHtml+=datar["m1_ttl"].ToString()+"<br/>";

        }*/
        Panel pnl_menuset, pnl_submenu;
        HyperLink a_m0, a_m1;
        while (datar.Read())
        {
            string s_m0 = datar["m0_id"].ToString();

            pnl_menuset = (Panel)div_menu.FindControl("div_menuset_" + s_m0);
            if (pnl_menuset == null)
            {
                pnl_menuset = new Panel(); pnl_menuset.CssClass = "menuset";
                pnl_menuset.ID = "div_menuset_" + s_m0;
                div_menu.Controls.Add(pnl_menuset);
            }
            pnl_menuset = (Panel)div_menu.FindControl("div_menuset_" + s_m0);

            a_m0 = (HyperLink)FindControl("a_m0_" + s_m0);
            if (a_m0 == null)
            {
                a_m0 = new HyperLink(); a_m0.CssClass = "m0";
                a_m0.ID = "a_m0_" + s_m0;
                a_m0.Text = datar["m0_ttl"].ToString();
                a_m0.NavigateUrl = datar["m0_url"].ToString();
                pnl_menuset.Controls.Add(a_m0);
            }

            if (!(datar["m1_ttl"] is DBNull)){
                pnl_submenu = (Panel)FindControl("div_submenu_" + s_m0);
                if (pnl_submenu == null){
                    pnl_submenu = new Panel();
                    pnl_submenu.CssClass = "submenu";
                    pnl_submenu.ID = "div_submenu_" + s_m0;
                    pnl_menuset.Controls.Add(pnl_submenu);
                }
                pnl_submenu = (Panel)FindControl("div_submenu_" + s_m0);

                a_m1 = new HyperLink(); a_m1.CssClass = "m1";
                a_m1.ID = "a_m1_" + datar["m1_id"].ToString();
                a_m1.Text = datar["m1_ttl"].ToString();
                a_m1.NavigateUrl = datar["m1_url"].ToString();
                pnl_submenu.Controls.Add(a_m1);
        
            }
        }

        cnn.Close();

        Panel p = new Panel();
        p.Style.Add("clear", "both");
        div_menu.Controls.Add(p);
    }
}

 

<script type="text/javascript" src="jquery-1.8.3.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("a.m0").mouseenter(function () {
                $(".submenu").hide();
                $("+.submenu", this).fadeIn(200);
            });

            $("#div_menu").mouseleave(function () {
                $(".submenu").fadeOut(200);
            });
        });
    </script>

    <style type="text/css">
     

    { margin:0px; padding:0px;}
        #div_menu { border: 1px solid silver;
                     background:url(imgs/back_head.jpg);
                      background-repeat:repeat-x;}
        a { display:block; text-decoration:none; color:Gray;}
        a:hover{ color:#930;}
        a.m0 { margin:10px 0px 10px 0px; 
                padding: 0px 10px 0px 10px;border:1px solid silver; }
        .menuset+.menuset a.m0{ border:1px solid silver; }
        a.m1+a.m1{ border-top:1px dashed silver;}
        a.m1{ margin:5px; padding :10px;
               font-size:12px; }
        
        .menuset { float:left; border:1px solid silver;}
        .submenu {  display: none; }
    </style>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="div_menu" runat="Server"></div>
    </div>
    </form>
</body>
</html>