2017-07-03 69 views
0

我試圖從MySql數據庫插入和檢索數據使用RESTful API。當我執行插入查詢時,我收到異常消息:連接需要有效並打開。MySql異常:連接需要有效並打開。 C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using MySql.Data; 


using WorkAppApi.Models; 

namespace WorkAppApi 
{ 
public class DBConn 
{ 
    private MySql.Data.MySqlClient.MySqlConnection conn; 

    public DBConn() 
    { 
     string myconnstr; 
     myconnstr = "server=192.168.0.103;uid=root;pwd=ganga10lfc;database=readingmeterdb"; 
     try 
     { 
      conn = new MySql.Data.MySqlClient.MySqlConnection(); 
      conn.ConnectionString = myconnstr; 
      conn.Open(); 
     } 
     catch (MySql.Data.MySqlClient.MySqlException ex) 
     { 

     } 


    } 

    public long addMachine(machines machinetosave) 
    { 


     String sqlstr = "INSERT INTO machines (machineDesc) VALUES ('" + machinetosave.MachineDesc + "')"; 
     MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlstr,conn); 

     cmd.ExecuteNonQuery(); 

     long id = cmd.LastInsertedId; 
     return id; 
    } 


} 
} 

「消息」: 「發生了錯誤。」 「ExceptionMessage」: 「連接必須是有效的和開放的。」 「ExceptionType」: 「System.InvalidOperationException」, 「堆棧跟蹤在MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()\ MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior行爲)上的MySql.Data.MySqlClient.MySqlCommand.CheckState() r \ n在WorkAppApi.DBConn.addMachine(machines machinetosave)in C:\ Users \ User \ Desktop \ WorkAppApi \ WorkAppApi \ DBConn.cs中:第41行\ r \ n在WorkAppApi.Controllers.MachinesController.Post(機器值)中C:\用戶\用戶\桌面\ WorkAppApi \ WorkAppApi \控制器\ MachinesController .cs:在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor的lambda_method(Closure,Object,Object [])\ r \ n上的第31行\ r \ n。在System.Web上的System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance,Object [] arguments)\ r \ n下的c__DisplayClassc.b__6(Object instance,Object [] methodParameters)\ r \ n。 Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2參數,CancellationToken cancellationToken)\ r \ n ---來自之前引發異常的位置的堆棧跟蹤結束--- \ r \ n在System.Runtime.CompilerServices .TaskAwaiter.ThrowForNonSuccess(任務任務)\ r \ n在System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\ r \ n System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n ---從以前位置拋出異常的堆棧跟蹤結束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)上的\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務塔斯k)\ r \ n在System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\ r \ n ---從前一個位置拋出異常的堆棧跟蹤結束--- \ r \ n在System。 Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()「

+0

在'DBConn()'中打開連接時,最有可能出現看不見的異常。將異常記錄在那裏,因爲這將爲您提供最可能的後續故障原因 –

回答

0

你應該打開它只有在你需要它:

public long addMachine(machines machinetosave) 
{ 
string myconnstr = "server=192.168.0.103;uid=root;pwd=ganga10lfc;database=readingmeterdb"; 
    try 
    { 
     MySql.Data.MySqlClient.MySqlConnection() conn = new MySql.Data.MySqlClient.MySqlConnection(); 
     conn.ConnectionString = myconnstr; 
     conn.Open(); 

    String sqlstr = "INSERT INTO machines (machineDesc) VALUES ('" + machinetosave.MachineDesc + "')"; 
     MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlstr,conn); 

     cmd.ExecuteNonQuery(); 

     long id = cmd.LastInsertedId; 
     return id; 

    } 
    catch (MySql.Data.MySqlClient.MySqlException ex) 
    { 

    } 


} 

在側面說明,你真的應該用using(){}塊編碼,以保證開放資源的清理/關閉。