将 ASP.NET 连接到 Amazon RDS MariaDB
Connect ASP.NET to Amazon RDS MariaDB
前言: 我已经并且可以从 MySQL Workbench 连接到 Amazon RDS 中的相应数据库因此呈现我的用户名,实例 url、端口号和密码要正确。
我正在为 ASP.NET 创建在线应用程序并且需要连接到 Amazon RDS
s MariaDB 代替。我尝试通过 web.config 或 c# 代码方式来完成,但两者都不起作用。需要建议。
方法一
Web.config:
<add name="rdbs" connectionString="Server=xxxxxx.xxxxxx.ap-southeast-1.rds.amazonaws.com:3306; Database=xxx; Uid=xxxx; Pwd=xxxx;" providerName="MySql.Data.MySqlClient"/>
对于我的 C# 代码端:
string connStr = ConfigurationManager.ConnectionStrings["rdbs"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(connStr))
方法二
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Initial Catalog"] = "xxxx";
builder["Data Source"] = "xxxxx.xxxx.ap-southeast-1.rds.amazonaws.com";
builder["integrated Security"] = true;
builder["Uid"] = "xxxx";
builder["Pwd"] = "xxxx";
string connexionString = builder.ConnectionString;
SqlConnection connexion = new SqlConnection(connexionString);
try { connexion.Open(); return true; }
catch { return false; }
这是我面临的错误形式:
留言"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)"
谢谢!
这实际上解决了它。
从 Nuget
中获取 MySql.Data.Entity
在web.config
中有这个
<add name="connRDB" connectionString="Data Source=xxxxx.xxxx.ap-southeast-1.rds.amazonaws.com;port=3306;Initial Catalog=xxxxx;User Id=xxxxx;password=xxxx" providerName="MySql.Data.MySqlClient" />
在代码方面
string constr = ConfigurationManager.ConnectionStrings["connRDB"].ConnectionString;
using (MySqlConnection conn = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("Select * FROM orders"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = conn;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
前言: 我已经并且可以从 MySQL Workbench 连接到 Amazon RDS 中的相应数据库因此呈现我的用户名,实例 url、端口号和密码要正确。
我正在为 ASP.NET 创建在线应用程序并且需要连接到 Amazon RDS s MariaDB 代替。我尝试通过 web.config 或 c# 代码方式来完成,但两者都不起作用。需要建议。
方法一
Web.config:
<add name="rdbs" connectionString="Server=xxxxxx.xxxxxx.ap-southeast-1.rds.amazonaws.com:3306; Database=xxx; Uid=xxxx; Pwd=xxxx;" providerName="MySql.Data.MySqlClient"/>
对于我的 C# 代码端:
string connStr = ConfigurationManager.ConnectionStrings["rdbs"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(connStr))
方法二
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Initial Catalog"] = "xxxx";
builder["Data Source"] = "xxxxx.xxxx.ap-southeast-1.rds.amazonaws.com";
builder["integrated Security"] = true;
builder["Uid"] = "xxxx";
builder["Pwd"] = "xxxx";
string connexionString = builder.ConnectionString;
SqlConnection connexion = new SqlConnection(connexionString);
try { connexion.Open(); return true; }
catch { return false; }
这是我面临的错误形式:
留言"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)"
谢谢!
这实际上解决了它。
从 Nuget
中获取 MySql.Data.Entity
在web.config
中有这个<add name="connRDB" connectionString="Data Source=xxxxx.xxxx.ap-southeast-1.rds.amazonaws.com;port=3306;Initial Catalog=xxxxx;User Id=xxxxx;password=xxxx" providerName="MySql.Data.MySqlClient" />
在代码方面
string constr = ConfigurationManager.ConnectionStrings["connRDB"].ConnectionString; using (MySqlConnection conn = new MySqlConnection(constr)) { using (MySqlCommand cmd = new MySqlCommand("Select * FROM orders")) { using (MySqlDataAdapter sda = new MySqlDataAdapter()) { cmd.Connection = conn; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } }