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

求解!!“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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试