c#将用户数据添加到本地数据库-解析错误

本文关键字:数据库 错误 用户 用户数 数据 添加 | 更新日期: 2023-09-27 18:02:48

我试图将用户输入添加到本地数据库,使用testbox, comboboxes, checkbox, checklistbox和datapickers,它真的不玩球。

我得到一个SQLException未处理的错误,说明

解析查询出错。[令牌行号= 1,令牌行偏移量= 261,令牌错误= End]' at the ' cmd.ExecuteNonQuery();

我的代码如下,有人知道我在哪里出错了吗?

private void button1_Click(object sender, EventArgs e)
{
    string query = "INSERT INTO OMTracker (ProjectNumber, ProjectTitle, ProjectDescription, Client, EndUser, ContractManager, Engineer, DocumentsNeeded, CompletedDate, IssuedDate, ContractDiary, Revision, Comments) VALUES (@ProjectNumber, @ProjectTitle, @ProjectDescription, @Client, @EndUser, @ContractManager, @Engineer, @DocumentsNeeded, @CompletedDate, @IssuedDate, @ContractDiary, @Revision, @Comments)";
    string connectionString = @"Data Source=|DataDirectory|'OMTrackerDatabase.sdf;Persist Security Info=True";
    SqlCeConnection connection = new SqlCeConnection(connectionString);
    connection.Open();
    SqlCeCommand cmd = new SqlCeCommand(query, connection);
    string Documents = DocumentsNeeded.CheckedItems.ToString();
    cmd.Parameters.AddWithValue("@ProjectNumber", ProjectNumber.Text);
    cmd.Parameters.AddWithValue("@ProjectTitle", ProjectTitle.Text);
    cmd.Parameters.AddWithValue("@ProjectDescription", ProjectDescription.Text);
    cmd.Parameters.AddWithValue("@Client", Client.Text);
    cmd.Parameters.AddWithValue("@EndUser", EndUser.Text);
    cmd.Parameters.AddWithValue("@ContractManager", ContractManager.SelectedValue);
    cmd.Parameters.AddWithValue("@Engineer", Engineer.SelectedValue);
    cmd.Parameters.AddWithValue("@DocumentsNeeded", Documents);
    cmd.Parameters.AddWithValue("@Revision", Revision.Text);
    cmd.Parameters.AddWithValue("@CompletedDate", CompletedDate.Value);
    cmd.Parameters.AddWithValue("@IssuedDate", IssuedDate.Text);
    cmd.Parameters.AddWithValue("@ContractDiary", ContractDiary.Checked ? 1 : 0);
    cmd.Parameters.AddWithValue("@Comments", Comments.Text);
    cmd.ExecuteNonQuery();          
    MessageBox.Show("Entry Added.");
    this.Close();
}

c#将用户数据添加到本地数据库-解析错误

  cmd.Parameters.AddWithValue("@CompletedDate", CompletedDate.Value);
    cmd.Parameters.AddWithValue("@IssuedDate", IssuedDate.Text);

——我猜你正试图将一个字符串放入IssuedDate的datetime字段。

我明白了。它不喜欢'SelectedValue'作为@ContractManager和@Engineer的参数值,所以我把它改成'SelectedItem',它工作得很好。

谢谢你的建议。