c#数据库选择,然后插入但是值是0

cyej8jka  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(455)

我试图从表1中选择并插入一个datetime到表2。我已成功插入数据。但是,表2中显示的日期时间是0000-00-00:00:00。idk哪里是错误。有人请帮我解决这个问题。我正在努力解决这个问题。这是选择然后插入的正确方法吗(从表1中选择,然后插入表2中)

try
    {
        string myConnectionString;
        myConnectionString= "server=localhost;uid=root;pwd=root;database=medicloud;SslMode=None;charset=utf8";

        MySqlConnection connection = new MySqlConnection(myConnectionString);
        MySqlCommand cmd = new MySqlCommand();

        cmd.CommandType = CommandType.Text;

        EncodingProvider ppp;
        ppp = CodePagesEncodingProvider.Instance;
        Encoding.RegisterProvider(ppp);

        connection.Open();   

        string select = "Select time from assign where userId=@name";
        cmd.Parameters.AddWithValue("@name", txtValue.Text);                           
        cmd.CommandText = select;
        cmd.Connection = connection;

        MySqlDataReader selectAssign = cmd.ExecuteReader();
        selectAssign.Read();
        string assign = (selectAssign["time"].ToString());
        selectAssign.Close();

        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT into bluetooth (userId,arm,armNumberDone,armNumber,comDate,assignDate,status) VALUES (@name, @stupid0, @stupid1, @stupid2, @stupid3, @stupid4, @stupid5)";
        cmd.Parameters.AddWithValue("@stupid0", databaseLine);
        cmd.Parameters.AddWithValue("@stupid1", counter);
        cmd.Parameters.AddWithValue("@stupid2", databaseValue);
        cmd.Parameters.AddWithValue("@stupid3", DateTime.Now);
        cmd.Parameters.AddWithValue("@stupid4", assign);
        cmd.Parameters.AddWithValue("@stupid5", complete);
        cmd.Connection = connection;

        cmd.ExecuteNonQuery();
        connection.Close();

    }

    catch (MySqlException ex)
    {
        txtExercise.Text = ex.ToString();
    }
5cnsuln7

5cnsuln71#

cmd.Parameters.AddWithValue("@stupid3", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
cmd.Parameters.AddWithValue("@stupid4", GetDateString(assign));

有这样的方法:

public static string GetDateString(string date)
{
    DateTime theDate;
    if (DateTime.TryParseExact(date, "dd/MM/yyyy HH:mm:ss", 
            CultureInfo.InvariantCulture, DateTimeStyles.None, out theDate))
    {
        // the string was successfully parsed into theDate
        return theDate.ToString("dd/MM/yyyy HH:mm:ss");
    }
    else
    {
        // the parsing failed, return some sensible default value
        return string.Empty;
    }
}
bzzcjhmw

bzzcjhmw2#

请试试这个

try
{
      string myConnectionString;
      myConnectionString = "server=localhost;uid=root;pwd=root;database=medicloud;SslMode=None;charset=utf8";

      MySqlConnection connection = new 
           MySqlConnection(myConnectionString);
      MySqlCommand cmd = new MySqlCommand();

      cmd.CommandType = CommandType.Text;

      EncodingProvider ppp;
      ppp = CodePagesEncodingProvider.Instance;
      Encoding.RegisterProvider(ppp);

      connection.Open();

      string select = "Select time from assign where userId=@name";
      cmd.Parameters.AddWithValue("@name", txtValue.Text);
      cmd.CommandText = select;
      cmd.Connection = connection;

      MySqlDataReader selectAssign = cmd.ExecuteReader();
      selectAssign.Read();
      string assign = (selectAssign["time"].ToString());
      selectAssign.Close();

      DateTime assignDate = DateTime.Now;
      DateTime.TryParseExact(assign, out assignDate);

      cmd.CommandType = CommandType.Text;
      cmd.CommandText = "INSERT into bluetooth 
(userId,arm,armNumberDone,armNumber,comDate,assignDate,status) VALUES (@name, 
@stupid0, @stupid1, @stupid2, @stupid3, @stupid4, @stupid5)";
      cmd.Parameters.AddWithValue("@stupid0", databaseLine);
      cmd.Parameters.AddWithValue("@stupid1", counter);
      cmd.Parameters.AddWithValue("@stupid2", databaseValue);
      cmd.Parameters.AddWithValue("@stupid3", DateTime.Now);
      cmd.Parameters.AddWithValue("@stupid4", assignDate);
      cmd.Parameters.AddWithValue("@stupid5", complete);
      cmd.Connection = connection;

      cmd.ExecuteNonQuery();
      connection.Close();
   }
  catch (MySqlException ex)
  {
      txtExercise.Text = ex.ToString();
  }
}
cgyqldqp

cgyqldqp3#

你需要使用 .ExecuteReader() 使用 .Read() 移动到结果集中的每一行。如果您确定只返回一行,请使用 .ExecuteScalar() 相反。研究两者在网上的差异。下面是一个使用 .ExecuteReader() .
我也重新写了使用 using 语句可以简化一点,但不会与原始代码偏离太多,因此您不必担心关闭和处理资源,因为它们继承自 IDisposable 一旦他们退出游戏就会自动完成 using 块:

string assign = DateTime.Now.ToString();

string myConnectionString;
    myConnectionString= "server=localhost;uid=root;pwd=root;database=medicloud;SslMode=None;charset=utf8";

string select = "Select time from assign where userId=@name";

using (MySqlConnection con = new MySqlConnection(myConnectionString))
{
    using (MySqlCommand cmd = new MySqlCommand(select))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        cmd.Parameters.AddWithValue("@name", txtValue.Text);                      

        using (MySqlDataReader cursor = cmd.ExecuteReader())
        {
            while (cursor.Read())
            {
                assign = cursor["time"];
            }
        }
    }

    string insert = "INSERT into bluetooth (userId,arm,armNumberDone,armNumber,comDate,assignDate,status) VALUES (@name, @stupid0, @stupid1, @stupid2, @stupid3, @stupid4, @stupid5)";

    using (MySqlCommand cmd = new MySqlCommand(insert))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        cmd.Parameters.AddWithValue("@stupid0", databaseLine);
        cmd.Parameters.AddWithValue("@stupid1", counter);
        cmd.Parameters.AddWithValue("@stupid2", databaseValue);
        cmd.Parameters.AddWithValue("@stupid3", DateTime.Now);
        cmd.Parameters.AddWithValue("@stupid4", assign);
        cmd.Parameters.AddWithValue("@stupid5", complete);

        cmd.ExecuteNonQuery();
    }
}

相关问题