我试图选择然后从表1插入日期时间到表2.我已成功插入数据.但是,表2中显示的日期时间为00:00:00. Idk哪里出错.有人请帮我解决这个问题.我正在努力解决这个问题.这是SELECT然后插入的正确方法吗? (从表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();
}
最佳答案 请试试这个
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();
}
}