using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;//这个是添加odbc引用

namespace UseDSN
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            OdbcConnection odbcCon =//创数据库连接对象
                new OdbcConnection("DSN=MyData;uid=sa;pwd=123456");
            try
            {
                OdbcDataAdapter odbcDat =//创建数据适配器
                    new OdbcDataAdapter("SELECT * from test", odbcCon);
                DataTable dt = new DataTable("帐单");//创建数据表
                odbcDat.Fill(dt);//填充数据表
                this.dataGridView1.DataSource = dt.DefaultView;//设置数据源
            }
            catch (Exception ey)//捕获异常
            {
                MessageBox.Show(ey.Message);//弹出消息对话框
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐