"列名无效"尝试使用SQL将数据插入数据库时

时间:2016-06-27 10:30:16

标签: c# sql asp.net sql-server

我尝试使用visual studio将新值插入到我的数据库中,这是我的代码:

SqlCommand cmd = new SqlCommand();

SqlConnection conn = new SqlConnection();
conn.ConnectionString = dbconnectionstring;
conn.Open();

cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Data_ConfigInfo (TypeID, Name, ValidFromDate, ValidToDate, ValidFromSOP, ValidToSOP, Description)" +
                  "VALUES(@TypeID, @Name, @ValidFromDateTime, @ValidToDateTime, @ValidFromSOP, @ValidToSOP, @Description)";

cmd.Parameters.AddWithValue("TypeID", Logg.TypeID);
cmd.Parameters.AddWithValue("Name", Logg.Name);

if(Logg.ValidFrom.Equals(null)) {
     cmd.Parameters.AddWithValue("@ValidFromDateTime", DBNull.Value);
}
else
{
    cmd.Parameters.AddWithValue("@ValidFromDateTime", Logg.ValidFrom);
}

if (Logg.ValidTo.Equals(null))
{
    cmd.Parameters.AddWithValue("@ValidToDateTime", DBNull.Value);
}
else
{
    cmd.Parameters.AddWithValue("@ValidToDateTime", Logg.ValidFrom);
}

cmd.Parameters.AddWithValue("@ValidFromSOP", Logg.ValidFromSOP);
cmd.Parameters.AddWithValue("@ValidToSOP", Logg.ValidToSOP);
cmd.Parameters.AddWithValue("@Description", Logg.Description);

cmd.ExecuteNonQuery();
conn.close();
conn.dispose();

但是在

cmd.ExecuteNonQuery(); 

我收到错误

  

ValidToDateTime和ValidFromDateTime的列名无效。

两者都是日期时间变量。我似乎无法找到错误。有任何想法吗?

这是我的数据表的外观

Datatable structure

2 个答案:

答案 0 :(得分:5)

您的数据库表格中包含ValidFromDateTime, ValidToDateTime列,但INSERT语句中的列列表中包含ValidFromDate, ValidToDate。在这里发帖时你确定这不是一个错字吗?

答案 1 :(得分:1)

你忘了'@'吗?

  let currentHTML = stateToHTML(this.state.editorState.getCurrentContent());
  currentHTML =   currentHTML + "<li>" + value +"</li>"
  let contentState = stateFromHTML(currentHTML);
  this.setState({editorState : editorState.createWithContent(contentState)});