我已经创建了一个REST,它试图连接到数据库并执行一个SELECT语句,仅此而已。如果成功,返回的String应该是,OK,,否则它应该返回FAIL,并包含一些异常细节(设置在catch中)。
Server = "google IP",
Port = 3306,
UserID = "username",
Password = "Your Password",
Database = "database"不需要为某些IP开放,因为API在上
MySqlConnection connection = new MySqlConnection(connection.connectionstring);
try
{
String query = "select * from tablename";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
command.ExecuteNonQuery();
value = "OK";
connection.Close();
}
catch (Exception ex)
{
value = "FAIL" + ex.Message + ex.Source + ex.InnerException + ex.Data;
}
finally
{
connection.Close();
}我已经试过了MySql.Data Mysql连接器
我得到的错误结果如下:
Connect Timeout expired.MySqlConnectorSystem.ObjectDisposedException: Safe handle has been closed
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Interop.Sys.TryChangeSocketEventRegistration(IntPtr port, SafeHandle socket, SocketEvents currentEvents, SocketEvents newEvents, IntPtr data)
at System.Net.Sockets.SocketAsyncContext.Register()
at System.Net.Sockets.SocketAsyncContext.OperationQueue`1.StartAsyncOperation(SocketAsyncContext context, TOperation operation, Int32 observedSequenceNumber)
at System.Net.Sockets.SocketAsyncContext.PerformSyncOperation[TOperation](OperationQueue`1& queue, TOperation operation, Int32 timeout, Int32 observedSequenceNumber)
at System.Net.Sockets.SocketAsyncContext.Connect(Byte[] socketAddress, Int32 socketAddressLen)
at System.Net.Sockets.SocketPal.Connect(SafeCloseSocket handle, Byte[] socketAddress, Int32 socketAddressLen)
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPAddress address, Int32 port)
at MySqlConnector.Core.ServerSession.OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 775System.Collections.ListDictionaryInternal发布于 2019-02-06 15:57:03
你从不给连接分配任何连接字符串.
似乎不太可能成功。见文档。
https://stackoverflow.com/questions/54555814
复制相似问题