2012-02-18 60 views
0

Visual Studio 2010的C#插入在MySQL數據庫中的二進制數據量巨大,但插入時,我收到以下錯誤mysql中的數據:插入與我使用C#

Exception is+ The connection has been disabled.StackTrace= at System.Data.Odbc.OdbcConnection.ConnectionIsAlive(Exception innerException) at System.Data.Odbc.OdbcConnection.HandleErrorNoThrow(OdbcHandle hrHandle, RetCode retcode) at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) at System.Data.Odbc.OdbcCommand.ExecuteNonQuery() at UploadFile.btnFileUpload_Click1(Object sender, EventArgs e) in c:\inetpub\wwwroot\computingClient\UploadFile.aspx.cs:line 133

mysql的數據類型是LONGBLOB 這裏是我的代碼:

string connStr = ConfigurationManager.AppSettings["connectionString"]; 
OdbcConnection conn = new OdbcConnection(connStr); 
conn.Open(); 
OdbcCommand cmd = conn.CreateCommand(); 
string sql = "insert into uploadeddata values(?,?,?,?,?)"; 
//cmd.CommandText = "insert into uploadeddata values(0 ,'" + fileName + "'," + id +",'"+strTokens +"')"; 
cmd.CommandText = sql; 
cmd.Parameters.AddWithValue("@id", 0); 
cmd.Parameters.AddWithValue("@filename", fileName); 
cmd.Parameters.AddWithValue("@ServerKey", id); 
cmd.Parameters.AddWithValue("@Token", strTokens); 
cmd.Parameters.AddWithValue("@userId", Session["LoggedInUserId"]); 
//Logger.writeLog(@"D:\Cloud Computing\CloudClient\Log\Log.txt","id:"+id+",filename:"+fileName+",userid:"+Session["LoggedInUserId"]+",strtokens:"+strTokens); 
cmd.ExecuteNonQuery(); 
conn.Close(); 
progressCounter = 100; 
//progress.SecondaryValue = progressCounter; 
//progress.SecondaryPercent = progressCounter; 
string script = "<script>alert('File uploaded successfully');</script>"; 
ClientScript.RegisterClientScriptBlock(this.GetType(), script, script); 
+0

你在哪裏插入二進制數據? – Oybek 2012-02-18 11:01:26

+3

什麼版本的MySql,MySql 5和以上的版本支持名稱參數,你不需要像SqlServer那樣用「@」符號來加前綴。我還建議你看看使用Native .Net Connector而不是ODBC,因爲它是繼承的更快,並提供更好的TypeConversion語義。http://dev.mysql.com/downloads/connector/net/ – Lloyd 2012-02-18 11:19:50

+0

@Lloyd真的很好的評論。值得成爲一個答案! – Oybek 2012-02-18 12:32:20

回答

2

既然你要插入大量的數據,你或許應該增加max_allowed_packet在你的MySQL配置文件(和重啓MySQL,事後)。這個值通常在默認情況下很小,以防止發送給mysql的無效數據包,但是如果您希望像您這樣插入大量數據,您可以增加它,但請注意,協議限制爲1GB(因此,沒有比這大的數據包)。 Here你可以找到更多信息。

另外,正如@Lloyd指出的那樣,您應該使用.NET Connector而不是odbc驅動程序。在這種情況下,你的代碼應該看起來像這樣:

using(var connection = new MySqlConnection(ConfigurationManager.AppSettings["connectionString"])) 
{ 
    connection.Open(); 
    using(var command = connection.CreateCommand()) 
    { 
     command.CommandText = "INSERT INTO uploadeddata VALUES (@id, @filename, @serverKey, @token, @userId)"; 
     command.Parameters.Add(new MySqlParameter("id", 0)); 
     command.Parameters.Add(new MySqlParameter("filename", fileName)); 
     command.Parameters.Add(new MySqlParameter("serverKey", id)); 
     command.Parameters.Add(new MySqlParameter("token", strTokens)); 
     command.Parameters.Add(new MySqlParameter("userId", Session["LoggedInUserId"])); 

     command.ExecuteNonQuery(); 
    } 
} 
+0

+1我忘記了數據包大小這是一個常見問題與斑點。 – Lloyd 2012-02-18 14:29:23