问题更新或从Visual C#插入mySQL [英] Problem Update or insert mySQL from Visual C#

查看:95
本文介绍了问题更新或从Visual C#插入mySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上。我在Update中遇到问题或从Visual C#插入mySQL。情况是用户写在datagridview的特定行,列中,我正在使用单元格结束编辑事件将数据插入到数据库中。这是插入和更新的代码。



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

conn.Open();
if (!string.IsNullOrEmpty(mainClass.dbMetName.Rows [rowIndex] .Cells [ 1 ]。Value.ToString()))
{
command.CommandText = SELECT COUNT (*)FROM + database +
。代谢产品 +
WHERE MetaboliteID = +
' + metNo + ;
command.ExecuteNonQuery();
decimal count =( decimal )command.ExecuteScalar(); // 此处出现问题
if (count > 0
{
command.CommandText = UPDATE + database +
。代谢物 +
SET Metabolite_Name = +
' + metName + +
WH ERE 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);
}
}
}





我想在输入if else语句之前捕获计数。但错误说明具体演员表是无效的。如果我在输入if else语句之前声明int count =(int)command.ExecuteNonQuery(),则无论如何都返回-1值。

解决方案

为什么你是两次执行此查询?另外问题似乎是你的查询返回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;


这篇关于问题更新或从Visual C#插入mySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆