lingdang_szu 2016-11-19 15:21 采纳率: 0%
浏览 951

小白,求大神解决一下,谢谢O(∩_∩)O谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lesson2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: 这行代码将数据加载到表“students.Student”中。您可以根据需要移动或删除它。
        this.studentTableAdapter.Fill(this.students.Student);
        DataGridViewDataLoad();
        ComboBoxDataLoad();
    }
    private void DataGridViewDataLoad(string sql = "select * from Student")
    {
        //创建SqlConnection、SqlDataAdapter和 DataSet的对象
        SqlConnection con = new SqlConnection("server=YUANZHANG-PC\\SQLSERVER; Initial Catalog=itcast;Integrated Security =true;");
        SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        con.Open();
        //获取并填充数据
        adapter.Fill(ds);
        con.Close();
        //将获取的数据设置为控件的数据源
        dataGridView1.DataSource = ds.Tables[0];
    }
    /// <summary>
    /// 加载ComboBox中的数据
    /// </summary>
    private void ComboBoxDataLoad()
    {
        //清空ComboBox,防止重复绑定
        cmbSubject.Items.Clear();
        cmbSubject.Items.Add("全部");
        cmbSubject.SelectedIndex = 0;
        SqlConnection con = new SqlConnection("server=YUANZHANG-PC\\SQLSERVER;database=itcast;uid=sa;pwd=13201319");
        string sql = "select distinct subject from Student";
        SqlCommand cmd = new SqlCommand(sql, con);
        con.Open();-->显示这个地方有问题!问题如下!
        SqlDataReader reader = cmd.ExecuteReader();
        //reader的HasRows属性表示是否有返回数据
        if (reader.HasRows)
        {
            //Read()方法表示读取下一条记录,读到数据返回True,否则返回false
            while (reader.Read())
            {
                //将读取的第一列数据添加的下拉列表项中
                cmbSubject.Items.Add(reader[0]);
            }
        }
        //关闭对象
        reader.Close();
        con.Close();
    }
}

}

问题:
“System.Data.SqlClient.SqlException”类型的未经处理的异常在 System.Data.dll 中发生 其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接)

谢谢大神!T^T

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog