ASP.NET链接离线MDF

本文介绍如何将数据库文件从现有数据库中分离,并在ASP.NET页面中通过配置和代码实现数据读取的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

第一步:在需要的数据库文件.mdf从数据库中分离出来,然后拷贝到App_data文件夹中

第二步:然后在web.config中,进行编辑链接字符串:

<connectionStrings>

       <addname="demoConnect"connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\demo.mdf;Integrated Security=True"providerName="System.Data.SqlClient"/>

       <!--<add name="demoConnect" connectionString="Data Source=127.0.0.1;Initial Catalog=demo;Integrated Security=True" providerName="System.Data.SqlClient"/>-->

    </connectionStrings>

 

第三步:在app_code文件夹中,新建一个类,用全全局的改变和有关的设置

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

 

///<summary>

/// Summary description for DbUtil

///</summary>

public static class DbUtil

{

    public static SqlConnection GetSqlConnection()

    {

        string connectString = ConfigurationManager.ConnectionStrings["demoConnect"].ConnectionString;

        //2.创建SqlConnection对象

        SqlConnection con = new SqlConnection(connectString);

        return con;

    }

}

public class Student

{

    public string No { get; set; }

    public string Name { get; set; }

    ///<summary>

    ///

    ///</summary>

    ///<returns></returns>

    public static IList<Student> GetStudents()

    {

        SqlConnection conn = DbUtil.GetSqlConnection();

        DataSet ds = new DataSet();

        SqlDataAdapter dtp = new SqlDataAdapter("select no,name from StudentInfo ", conn);

        dtp.Fill(ds, "st");

        List<Student> rels = new List<Student>();

        foreach (DataRow row in ds.Tables["st"].Rows)

        {

            Student st = new Student() { Name = row["name"].ToString(), No = row["no"].ToString() };

            rels.Add(st);

        }

        return rels;

    }

}

 

第四步:在建立的aspx页面中,添加一个button,并在其事件中进行调用

protected void btnReadDb_Click(object sender, EventArgs e)

    {

        SqlConnection conn = null;

        try

        {

            this.lblInfo.Text = "";

            //1.获取连接字符串

            //string connectString = ConfigurationManager.ConnectionStrings["demoConnect"].ConnectionString;

            //2.创建SqlConnection对象

            //SqlConnection con = new SqlConnection(connectString);

            SqlConnection con = DbUtil.GetSqlConnection();

            //3 .创建SqlCommand对象

            SqlCommand cmd = new SqlCommand();

            //4。设置cmd命令信息

            cmd.CommandText = "select * from studentInfo";           

            cmd.Connection = con;

            //5.打开数据库

            con.Open();

            //6操作数据库

            SqlDataReader result = cmd.ExecuteReader();

            while(result.Read())

            {

                if (this.lblInfo.Text!="")

                {

                    this.lblInfo.Text += ",";

                }

                string strTmp = string.Format("[姓名:{0},学号:{1}]", result["name"], result["no"]);

                 this.lblInfo.Text +=strTmp;

            }

            //7关闭数据库

            con.Close();

        }

        catch (System.Exception ex)

        {

 

        }

        finally

        {

            if (conn != null && conn.State != ConnectionState.Closed)

            {

                conn.Close();

            }

        }

}

 

补充:VS提供的命名空间 System.Configuration

 

using System;

using System.Collections.Specialized;

 

namespace System.Configuration

{

    // 摘要:

    //     提供对客户端应用程序配置文件的访问。无法继承此类。

    public static class ConfigurationManager

    {

        // 摘要:

        //     获取当前应用程序默认配置的 System.Configuration.AppSettingsSection 数据。

        //

        // 返回结果:

        //     返回一个 System.Collections.Specialized.NameValueCollection 对象,该对象包含当前应用程序默认配置的

        //     System.Configuration.AppSettingsSection 对象的内容。

        //

        // 异常:

        //   System.Configuration.ConfigurationErrorsException:

        //     无法使用应用程序设置数据检索 System.Collections.Specialized.NameValueCollection 对象。

        public static NameValueCollection AppSettings { get; }

        //

        // 摘要:

        //     获取当前应用程序默认配置的 System.Configuration.ConnectionStringsSection 数据。

        //

        // 返回结果:

        //     返回一个 System.Configuration.ConnectionStringSettingsCollection 对象,该对象包含当前应用程序默认配置的

        //     System.Configuration.ConnectionStringsSection 对象的内容。

        //

        // 异常:

        //   System.Configuration.ConfigurationErrorsException:

        //     无法检索 System.Configuration.ConnectionStringSettingsCollection 对象。

        public static ConnectionStringSettingsCollection ConnectionStrings { get; }

 

        // 摘要:

        //     检索当前应用程序默认配置的指定配置节。

        //

        // 参数:

        //   sectionName:

        //     配置节的路径和名称。

        //

        // 返回结果:

        //     指定的 System.Configuration.ConfigurationSection 对象,或者,如果该节不存在,则为 null。

        //

        // 异常:

        //   System.Configuration.ConfigurationErrorsException:

        //     未能加载配置文件。

        public static object GetSection(string sectionName);

        //

        // 摘要:

        //     将当前应用程序的配置文件作为 System.Configuration.Configuration 对象打开。

        //

        // 参数:

        //   userLevel:

        //     要打开配置的 System.Configuration.ConfigurationUserLevel

        //

        // 返回结果:

        //     一个 System.Configuration.Configuration 对象。

        //

        // 异常:

        //   System.Configuration.ConfigurationErrorsException:

        //     未能加载配置文件。

        public static Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel);

        //

        // 摘要:

        //     将指定的客户端配置文件作为 System.Configuration.Configuration 对象打开。

        //

        // 参数:

        //   exePath:

        //     与可执行文件关联的配置文件的路径。

        //

        // 返回结果:

        //     一个 System.Configuration.Configuration 对象。

        //

        // 异常:

        //   System.Configuration.ConfigurationErrorsException:

        //     未能加载配置文件。

        public static Configuration OpenExeConfiguration(string exePath);

        //

        // 摘要:

        //     将当前计算机上的计算机配置文件作为 System.Configuration.Configuration 对象打开。

        //

        // 返回结果:

        //     一个 System.Configuration.Configuration 对象。

        //

        // 异常:

        //   System.Configuration.ConfigurationErrorsException:

        //     未能加载配置文件。

        public static Configuration OpenMachineConfiguration();

        //

        // 摘要:

        //     通过使用指定的文件映射和用户级别,可以将指定的客户端配置文件作为 System.Configuration.Configuration 对象打开。

        //

        // 参数:

        //   fileMap:

        //     一个 System.Configuration.ExeConfigurationFileMap 对象,该对象引用代替应用程序的默认配置文件使用的配置文件。

        //

        //   userLevel:

        //     要打开配置的 System.Configuration.ConfigurationUserLevel

        //

        // 返回结果:

        //     一个 System.Configuration.Configuration 对象。

        //

        // 异常:

        //   System.Configuration.ConfigurationErrorsException:

        //     未能加载配置文件。

        public static Configuration OpenMappedExeConfiguration(ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel);

        //

        // 摘要:

        //     通过使用指定的文件映射,将计算机配置文件作为 System.Configuration.Configuration 对象打开。

        //

        // 参数:

        //   fileMap:

        //     一个 System.Configuration.ExeConfigurationFileMap 对象,该对象引用代替应用程序的默认配置文件使用的配置文件。

        //

        // 返回结果:

        //     一个 System.Configuration.Configuration 对象。

        //

        // 异常:

        //   System.Configuration.ConfigurationErrorsException:

        //     未能加载配置文件。

        public static Configuration OpenMappedMachineConfiguration(ConfigurationFileMap fileMap);

        //

        // 摘要:

        //     刷新命名节,这样在下次检索它时将从磁盘重新读取。

        //

        // 参数:

        //   sectionName:

        //     要刷新的节的配置节名称或配置路径和节名称。

        public static void RefreshSection(string sectionName);

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值