[英]Aspx C# MySQL connection string not connecting using web.Config
I am not an expert by any means with C# and asp but I have managed to make it to the end of my site and deploy.我无论如何都不是 C# 和 asp 的专家,但我已经设法完成了我的网站并进行了部署。 Everything works fine locally including the MySQL connection.本地一切正常,包括 MySQL 连接。 However when I plug in the connection string given by my host I get error messages.但是,当我插入主机提供的连接字符串时,我收到错误消息。 I have looked everywhere to find a solution and even contacted the host who was no help at all.我到处寻找解决方案,甚至联系了根本没有帮助的主机。 I'm so frustrated and I know the best of the best are here.我很沮丧,我知道最好的都在这里。 I'm sure there is a more than one issue with all my code.我确信我的所有代码都存在不止一个问题。 I was basic HTML and JS and SQL up until like 3 weeks ago.直到大约 3 周前,我还是基本的 HTML、JS 和 SQL。 Than you everyone.比你们大家。
Web.Config file 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>
Here is the page (just a test page) the path is pages/abc.aspx is you want to see the error.这是页面(只是一个测试页面)路径是 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>
and here is the C# behind the page这是页面后面的 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();
}
}
}
And this is the code provided by my host to connect to the MySQL database.这是我的主机提供的用于连接到 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
It doesn't like the 'Driver' parameter of the connection string your host provided.它不喜欢您的主机提供的连接字符串的“驱动程序”参数。 Try this in your web.config, adapted from the mysql connector docs .在你的 web.config 中试试这个,改编自 mysql 连接器文档。
<add name="mySQLconn" connectionString="Server=skyhill.ipowermysql.com;Database=skyhill;Uid=skyhillweb;Pwd=yourpassword;" providerName="MySql.Data.MySqlClient"/>
您可以将旧版本的 MySQL dll 用于 .Net,它解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.