c# - Aspx C# MySQL 连接字符串未使用 web.Config 连接

标签 c# mysql asp.net web-config

无论如何我都不是 C# 和 asp 方面的专家,但我已经成功地完成了我的站点并进行了部署。本地一切正常,包括 MySQL 连接。但是,当我插入主机提供的连接字符串时,我收到错误消息。我到处寻找解决方案,甚至联系了根本没有帮助的主机。我很沮丧,我知道最好的都在这里。我确信我的所有代码都存在不止一个问题。直到大约 3 周前,我都是基本的 HTML、JS 和 SQL。比你们大家。

Web.Config 文件

<configuration>
   <connectionStrings>
 <add name="mySQLconn" connectionString="DRIVER={MySQL ODBC 5.1 Driver};server=skyhill.ipowermysql.com; user id=skyhillweb; password=******; database=skyhill; OPTION=3;"
     providerName="MySql.Data.MySqlClient" />
   </connectionStrings>
 <system.web>
   <customErrors mode="Off"/>
   <compilation debug="true"/>
   <authentication mode="None"/>
 </system.web>
 <system.webServer>
   <defaultDocument>
     <files>
       <add value="/pages/default.aspx"/>
     </files>
   </defaultDocument>
 </system.webServer>
</configuration>

这里是页面(只是一个测试页面)路径是pages/abc.aspx就是你要看的错误

<form id="form1" runat="server">
   <asp:GridView ID="gridAgent" runat="server" AutoGenerateColumns="True">
   </asp:GridView>
   <asp:TextBox ID="MessageBox" runat="server">
   </asp:TextBox>
</form>

这是页面背后的 C#

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.Common;
using MySql.Data.MySqlClient;

namespace SkhillWebApp.pages
{
   public partial class abc : System.Web.UI.Page
   {

      protected void Page_Load(object sender, EventArgs e)
      {
         MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["mySQLconn"].ConnectionString);
         string strSQL = "SELECT agentID, FirstName, LastName FROM agent";
         conn.Open();
         MySqlDataAdapter mydata = new MySqlDataAdapter(strSQL, conn);
         MySqlCommandBuilder cmd = new MySqlCommandBuilder(mydata);
         DataSet ds = new DataSet();

         mydata.Fill(ds);

         gridAgent.DataSource = ds;
         gridAgent.DataBind();
         conn.Close();
      }
   }
}

这是我的主机提供的用于连接到 MySQL 数据库的代码。

Start
<% 
Dim ConnectionString 
ConnectionString="DRIVER={MySQL ODBC 5.1 Driver}; SERVER=skyhill.ipowermysql.com; PORT=3306;" &_
"DATABASE=skyhill; USER=skyhillweb; PASSWORD=*password*; OPTION=3;"
%>
' End

最佳答案

它不喜欢您的主机提供的连接字符串的“Driver”参数。在你的 web.config 中试试这个,改编自 mysql 连接器 docs .

<add name="mySQLconn" connectionString="Server=skyhill.ipowermysql.com;Database=skyhill;Uid=skyhillweb;Pwd=yourpassword;" providerName="MySql.Data.MySqlClient"/> 

关于c# - Aspx C# MySQL 连接字符串未使用 web.Config 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19965533/

相关文章:

c# - IEnumerable 中每个项目的自定义 json 序列化

c# - 如何在 C# 中将字典的子集提取到另一个字典中?

c# - 使用单点登录机制创建 Web 服务的选项有哪些?

mysql - 使用非索引和复合索引

asp.net - 验证失败后如何防止页面跳转到顶部位置?

c# - 异步等待、异常处理和继续使用 TaskContinuationOptions.OnlyOnFaulted

mysql - 如何使用 UCI 机器学习存储库中的数据集

mysql - 在mysql中使用union删除?

c# - 防止VS调试器超时

c# - 如何在 C# 中获取表的 innerHTML