日期:2014-05-17  浏览次数:20613 次

连接 数据库 不上——求搭救
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Data.OleDb;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实验八
{
    public partial class Form1 : Form
    {
        static string connectionString = @"DNS=Dong;DBQ=H:\school\work\SQL\第08次上机报告\PZdb.mdf";

        public Form1()
        {
            InitializeComponent();
        }

        private void first_Click(object sender, EventArgs e)
        {
            OdbcConnection conObj = new OdbcConnection(connectionString);
            conObj.Open();

            OdbcCommand command = new OdbcCommand("select * form Student",conObj);
            
            
            OdbcDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                information.Text = reader.GetString(0);
                information.Text = reader.GetString(1);
                information.Text = reader.GetString(2);
                information.Text = reader.GetString(3);
                information.Text = reader.GetString(4);
            }
            reader.Close();
            conObj.Close();
        }

    }
}


------解决方案--------------------
那个DNS是在机器上创建的一个东东,换个机器没有对应项或者虽然是名称相同可是实际的链接对象不同也不能成功的。换成显式连接信息。