且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

问题更新或从Visual C#插入mySQL

更新时间:2023-02-08 12:32:53

为什么你是两次执行此查询?另外问题似乎是你的查询返回NULL导致异常。试试这个





 command.CommandText =   SELECT COUNT(*)FROM + database + 
。代谢物 +
WHERE MetaboliteID = +
' + metNo + ';
// command.ExecuteNonQuery();
object tCount = command.ExecuteScalar(); // 此处出现问题< / pre>
int count = 0 ;
if (tCount!= null
count =( INT 跨度>)TCOUNT;


Hai. I'm having problem in Update or insert into mySQL from Visual C#. The situation is that user written inside specific row,column of datagridview and I'm using cell end edit event to insert the data inside database. Here is the code of insert and update.

class UpdateMetabolite: UpdateDatabase
    {
        public UpdateMetabolite(frmMain mainClass)
            : base(mainClass)
        {
            int rowIndex = mainClass.dbMetName.CurrentCell.RowIndex;
            try
            {
                string metNo = mainClass.dbMetName.Rows[rowIndex].Cells[0].Value.ToString();
                string metName = mainClass.dbMetName.Rows[rowIndex].Cells[1].Value.ToString();

                conn.Open();
                if (!string.IsNullOrEmpty(mainClass.dbMetName.Rows[rowIndex].Cells[1].Value.ToString()))
                {
                    command.CommandText = "SELECT COUNT(*) FROM " + database +
                                          ".Metabolites " +
                                          "WHERE MetaboliteID = " +
                                           "'" + metNo + "'";
                    command.ExecuteNonQuery();
                    decimal count = (decimal)command.ExecuteScalar(); //the problem occur here
                    if (count > 0)
                    {
                        command.CommandText = "UPDATE " + database +
                                              ".Metabolites " +
                                              "SET Metabolite_Name = " +
                                              "'" + metName + "'" +
                                              "WHERE MetaboliteID = " +
                                              "'" + metNo + "'";
                        command.ExecuteNonQuery();
                    }
                    else
                    {
                        command.CommandText = "INSERT INTO " + database +
                                              ".Metabolites " +
                                              "(MetaboliteID, Metabolite_Name)" +
                                              "VALUES " +
                                              "('" + metNo + "','" + metName + "');";
                        command.ExecuteNonQuery();
                    }
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }



I want to capture the count before enter if else statement. but the error stated specific cast is not valid. If I declare int count = (int)command.ExecuteNonQuery() before enter if else statement, it returns -1 value no matter what.

Why you are twice executing this query? In addition problem seems that your query returning NULL causing exception. Try this


command.CommandText = "SELECT COUNT(*) FROM " + database +
                                          ".Metabolites " +
                                          "WHERE MetaboliteID = " +
                                           "'" + metNo + "'";
  //command.ExecuteNonQuery();
  object tCount = command.ExecuteScalar(); //the problem occur here</pre>
  int count = 0;
  if (tCount != null)
      count = (int)tCount;