qq_25844033 2015-03-18 07:54 采纳率: 0%
浏览 9051

求解!!“System.Data.SqlClient.SqlException”类型的异常

使用vs调试网站时候出现一下问题。

“System.Data.SqlClient.SqlException”类型的异常在 System.Data.dll 中发生,但未在用户代码中进行处理

其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)

代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class UserController_top : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Bind();
}
}

/// <summary>
/// 按照影片的点击排行,
/// </summary>
public void Bind()
{
    string str = "select top 10 * from Movies order by MovieHit desc";
    SqlConnection con = DB.getConnection();
    DataSet ds = new DataSet(); //多行数据填充到数据集中
    SqlDataAdapter sda = new SqlDataAdapter(str, con);
    sda.Fill(ds, "Movie");
    this.DataList1.DataSource = ds.Tables["Movie"].DefaultView;
    DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "Play"){
    string hit = (e.Item.FindControl("Label8") as Label).Text;

    string str = (e.Item.FindControl("Lable20") as Label).Text;

    int id = Convert.ToInt32(str); //影片的ID号
    int num = Convert.ToInt32(hit) + 1; //以前的播放次数加上1就为当前的播放次数

    SqlConnection con = DB.getConnection();
    con.Open();
    SqlCommand cmd = new SqlCommand("update Movies set MovieHit=" + num + " where MovieID=" + id + "", con);
    cmd.ExecuteNonQuery();
    con.Close();
    Response.Redirect("Play.aspx?ID=" + id);

    }
}

}

问题代码:
sda.Fill(ds, "Movie");

软件版本:visualstudio2013+mysql5.6

  • 写回答

3条回答 默认 最新

  • 鸿运至尊 2015-03-18 08:12
    关注

    你检查一下变量 con ,可能是数据库没有正确的连接!

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)