使用ASP.net从mysql数据库读取数据时出错

时间:2017-05-13 12:38:24

标签: c# mysql asp.net

我尝试使用ASP.net从mysql数据库读取数据,如下所示 但我收到消息错误

  

发生与网络相关或特定于实例的错误   建立与SQL Server的连接。找不到服务器或   无法访问。验证实例名称是否正确   SQL Server配置为允许远程连接。 (提供者:命名   管道提供程序,错误:40 - 无法打开与SQL Server的连接)

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.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;

namespace BankingDemo
{
    public partial class AccountInquiry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string connstr = @"Server=127.0.0.1;Database=Mysql;Uid=root;Pwd=system;";
            /*string ConnStr;
            ConnectionStringSettings ConnSet = ConfigurationManager.ConnectionStrings["ConnConfig"];

            if (ConnSet == null || string.IsNullOrEmpty(ConnSet.ConnectionString))
            {
                throw new Exception("Fatal error: missing connecting string in web.config file");
                ConnStr = ConnSet.ConnectionString;
            }*/

            string cmdtxt = @"select Name,CountryCode,District,Population from world.city";

            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();

            SqlCommand cmd = new SqlCommand(cmdtxt);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;

            string temp="";

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                temp += reader["Name"].ToString();
                temp += reader["CountryCode"].ToString();
                temp += reader["District"].ToString();
                temp += reader["Population"].ToString();
                temp += "<br />";
            }

            conn.Close();

            lbl_CityName.Text = temp;

        }
    }
}

下面的代码标记

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AccountInquiry.aspx.cs" Inherits="BankingDemo.AccountInquiry" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Banking Site (Demo)</title>
</head>
<body>
    <asp:Label ID="lbl_CityName" runat="server"></asp:Label>
</body>
</html>

我安装了.Net的MySQL连接器,如图enter image description here

所示

0 个答案:

没有答案