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);
你在哪裏插入二進制數據? – Oybek 2012-02-18 11:01:26
什麼版本的MySql,MySql 5和以上的版本支持名稱參數,你不需要像SqlServer那樣用「@」符號來加前綴。我還建議你看看使用Native .Net Connector而不是ODBC,因爲它是繼承的更快,並提供更好的TypeConversion語義。http://dev.mysql.com/downloads/connector/net/ – Lloyd 2012-02-18 11:19:50
@Lloyd真的很好的評論。值得成爲一個答案! – Oybek 2012-02-18 12:32:20