不支持关键字:ASP.NET MVC 应用程序中的 'port' 错误
Keyword not supported: 'port' error in ASP.NET MVC application
我正在尝试创建一个 API 服务器,我需要访问 MySQL 数据库。我有这段代码:
public static class Database
{
private const string ConnectionString = "Server=localhost;Port=3306;Database=contoso_inc;Uid=root;Pwd=f6e527xp;";
public static IEnumerable<User> Execute(string query)
{
using (var connection = new SqlConnection(ConnectionString))
{
var command = new SqlCommand(query, connection);
connection.Open();
var reader = command.ExecuteReader();
var result = new List<User>();
while (reader.Read())
{
var user = new User
{
year = (uint) reader.GetValue(0),
month = (string) reader.GetValue(1),
users_num = (uint) reader.GetValue(2)
};
result.Add(user);
}
reader.Close();
return result;
}
}
}
错误出现在这一行:connection.Open();
。所以显然我出于某种原因不能使用 Port。有什么问题?
P.S。我看过相关问题,但大部分属于Entity Framework话题。我这里没有DbContext
,所以没用。
端口在 Server 关键字中指定,在您的示例中使用:
"Server=localhost, 3306;Database=contoso_inc;Uid=root;Pwd=f6e527xp;"
查看 SqlConnection.ConnectionString
的文档,我没有看到任何“端口”密钥的迹象。如果需要指定端口,可以在Server
部分指定:
The name or network address of the instance of SQL Server to which to connect. The port number can be specified after the server name:
server=tcp:servername, portnumber
所以也许您想要:“Server=localhost,3306;...”或“Server=tcp:localhost,3306;...”- 如果您真的认为您确实需要指定端口全部.
我认为您使用了错误的连接驱动程序。
尝试安装这个 nuget 包
Install-Package MySql.Data -Version 8.0.26
那么,你可以这样做:
public static class Database
{
private const string ConnectionString = "Server=localhost;Port=3306;Database=contoso_inc;Uid=root;Pwd=f6e527xp;";
public static IEnumerable<User> Execute(string query)
{
using (var connection = new MySql.Data.MySqlClient.MySqlConnection(ConnectionString))
{
var command = new MySqlCommand(query, connection);
connection.Open();
var reader = command.ExecuteReader();
var result = new List<User>();
while (reader.Read())
{
var user = new User
{
year = (uint) reader.GetValue(0),
month = (string) reader.GetValue(1),
users_num = (uint) reader.GetValue(2)
};
result.Add(user);
}
reader.Close();
return result;
}
}
}
我正在尝试创建一个 API 服务器,我需要访问 MySQL 数据库。我有这段代码:
public static class Database
{
private const string ConnectionString = "Server=localhost;Port=3306;Database=contoso_inc;Uid=root;Pwd=f6e527xp;";
public static IEnumerable<User> Execute(string query)
{
using (var connection = new SqlConnection(ConnectionString))
{
var command = new SqlCommand(query, connection);
connection.Open();
var reader = command.ExecuteReader();
var result = new List<User>();
while (reader.Read())
{
var user = new User
{
year = (uint) reader.GetValue(0),
month = (string) reader.GetValue(1),
users_num = (uint) reader.GetValue(2)
};
result.Add(user);
}
reader.Close();
return result;
}
}
}
错误出现在这一行:connection.Open();
。所以显然我出于某种原因不能使用 Port。有什么问题?
P.S。我看过相关问题,但大部分属于Entity Framework话题。我这里没有DbContext
,所以没用。
端口在 Server 关键字中指定,在您的示例中使用:
"Server=localhost, 3306;Database=contoso_inc;Uid=root;Pwd=f6e527xp;"
查看 SqlConnection.ConnectionString
的文档,我没有看到任何“端口”密钥的迹象。如果需要指定端口,可以在Server
部分指定:
The name or network address of the instance of SQL Server to which to connect. The port number can be specified after the server name:
server=tcp:servername, portnumber
所以也许您想要:“Server=localhost,3306;...”或“Server=tcp:localhost,3306;...”- 如果您真的认为您确实需要指定端口全部.
我认为您使用了错误的连接驱动程序。 尝试安装这个 nuget 包
Install-Package MySql.Data -Version 8.0.26
那么,你可以这样做:
public static class Database
{
private const string ConnectionString = "Server=localhost;Port=3306;Database=contoso_inc;Uid=root;Pwd=f6e527xp;";
public static IEnumerable<User> Execute(string query)
{
using (var connection = new MySql.Data.MySqlClient.MySqlConnection(ConnectionString))
{
var command = new MySqlCommand(query, connection);
connection.Open();
var reader = command.ExecuteReader();
var result = new List<User>();
while (reader.Read())
{
var user = new User
{
year = (uint) reader.GetValue(0),
month = (string) reader.GetValue(1),
users_num = (uint) reader.GetValue(2)
};
result.Add(user);
}
reader.Close();
return result;
}
}
}