当前位置:编程学习 > C#/ASP.NET >>

ADO.NET数据库编程 利用应用程序配置文件进行数据的读取


//下面是我个人的应用程序配置文件App.config : 把下面的内容全部覆盖掉项目中App.config中的内容即可
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="AccessDatabase"
       connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=StoreMIS.mdb;Persist Security Info=false;"
       providerName="System.Data.OleDb" />
  </connectionStrings>
</configuration>

//添加下面绿色的两个命名空间的引用
//展开ADO.NET项目 ——> 在灰色的 “引用” 文件夹上面右键 ——> 添加引用 ——> .Net选项卡上面往下拉,找到System.Configuration 点击确定。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Configuration;
using System.Data.OleDb;

 

//下面开始代码编写

namespace ADO.NET
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        ConnectionStringSettings connAccess;   //用于映射连接字符串

        // Access
        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Clear(); //点击按钮的时候先清除listView1中旧的数据
            connAccess = ConfigurationManager.ConnectionStrings["AccessDatabase"];  //引号内是配置文件中连接字符串的名字
            if (connAccess != null)
            {
                string strCon = connAccess.ConnectionString;
                this.textBox1.Text = strCon;
                OleDbConnection conn = new OleDbConnection(strCon);
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    this.textBox1.Text = ex.Message.ToString();
                }
                if (conn.State == ConnectionState.Open)
                {
                    //从...\bin\Debug 目录下的 StoreMIS.mdb Access数据库中的userinfo表中取出数据
                    listView1.View = View.Details;   //设置ListView显示视图方式
                    listView1.Columns.Add("姓 名").Width = 90;   //添加列名并指定列宽
                    listView1.Columns.Add("密 码").Width = 100;
                    listView1.Columns.Add("角 色").Width = 150;

                    string sql = "select userName,password,roleName from userinfo";
                    OleDbCommand cmd = new OleDbCommand("", conn);
                    cmd.CommandText = sql;
                    OleDbDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        string[] items = { reader[0].ToString(), reader[1].ToString(), reader[2].ToString() };
                        ListViewItem lvst = new ListViewItem(items);
                        int num = listView1.Items.Count;
                        listView1.Items.Insert(num, lvst);
                    }
                    reader.Close();
                    conn.Close();

                    label1.ForeColor = Color.Red;
                    label1.Text = "连接Access数据库成功!";
                }
                else
                {
                    label1.ForeColor = Color.Red;
                    label1.Text = "连接Access数据库失败!";
补充:软件开发 , C# ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,