我正在尝试更新名为' schedulle'的表格中的数据。但每次控制转到包含double值的条件语句时,我都会收到错误:
数据截断列'总计'在第1行和第34行;串
我在下面的代码中收到此错误:
//Insert Data in Schedule
foreach (ScheduleData item in schedUpdate)
{
query = "UPDATE schedulle SET "
+" sch_day='"+ item.DayOfWeek + "',morning='"+ item.Morning + "',evening='"+ item.Evening + "',total='@t' WHERE emp_id_fk='"+global_ID+"'";
command = new MySqlCommand(query, connection, transaction);
if (item.Morning.Equals("set") && item.Evening.Equals("set"))
{
command.Parameters.AddWithValue("@t", 5.0);
}
if (item.Morning.Equals("set") && item.Evening.Equals("unset"))
{
command.Parameters.AddWithValue("@t", Convert.ToDouble(2.5));
}
if (item.Morning.Equals("unset") && item.Evening.Equals("set"))
{
command.Parameters.AddWithValue("@t", 2.5);
}
if (item.Morning.Equals("unset") && item.Evening.Equals("unset"))
{
command.Parameters.AddWithValue("@t", 0);
}
command.ExecuteNonQuery();
}
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
}
我的数据库架构如下:
CREATE TABLE payroll_db.schedulle (
sch_id INT(11) NOT NULL AUTO_INCREMENT,
emp_id_fk INT(11) NOT NULL,
sch_day VARCHAR(10) NOT NULL,
morning VARCHAR(10) NOT NULL DEFAULT 'unset',
evening VARCHAR(10) NOT NULL DEFAULT 'unset',
total DOUBLE(10, 3) NOT NULL,
PRIMARY KEY (sch_id),
INDEX emp_id_fk (emp_id_fk),
CONSTRAINT schedulle_ibfk_1 FOREIGN KEY (emp_id_fk)
REFERENCES payroll_db.employee(emp_id) ON DELETE CASCADE ON UPDATE CASCADE
)
ENGINE = INNODB
AUTO_INCREMENT = 64
AVG_ROW_LENGTH = 2340
CHARACTER SET ascii
COLLATE ascii_general_ci
ROW_FORMAT = DYNAMIC;
此外,'项目'是ScheduleData类的实例。 ScheduleData类在下面提到:
public class ScheduleData : INotifyPropertyChanged
{
private string dayOfWeek;
public string DayOfWeek
{
get { return dayOfWeek; }
set
{
if (dayOfWeek != value)
{
dayOfWeek = value;
OnPropertyChanged("DayOfWeek");
}
}
}
private string morning = "unset";
public string Morning
{
get { return morning; }
set
{
if (morning != value)
{
morning = value;
OnPropertyChanged("Morning");
}
}
}
private string evening = "unset";
public string Evening
{
get { return evening; }
set
{
if (evening != value)
{
evening = value;
OnPropertyChanged("Evening");
}
}
}
private double total;
public double Total
{
get { return total; }
set
{
if(total != value)
{
total = value;
OnPropertyChanged("Total");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
' SchedUpdate'财产如下:
private List<ScheduleData> schedUpdate;
public List<ScheduleData> SchedUpdate
{
get
{
return schedUpdate;
}
set
{
if (schedUpdate != value)
{
schedUpdate = value;
OnPropertyChanged("SchedUpdate");
}
}
}
我想我已经分享了有关我收到错误的代码的最大细节。请帮助解决此错误。
答案 0 :(得分:0)
您在参数名称qoutes
周围提供了@t
,这不是必需的。从查询@t
total=@t
移除qoutes
也不要在查询中使用参数化Sql连接值,就像添加参数@t