收到错误:查询输入必须至少包含一个表或查询 [英] getting the error: Query input must contain at least one table or query

查看:304
本文介绍了收到错误:查询输入必须至少包含一个表或查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:查询输入必须至少包含一个表或查询 我的代码是:

i get this error :Query input must contain at least one table or query my code is:

    using (OleDbConnection myCon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=timetabledata.accdb")){                       
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
                           string q = "INSERT INTO timehourly (teacherid,subjectid) Values ('@teacherID','@subjid')" + " WHERE hour='@i' AND dayid='@ds'";
                          cmd.Parameters.AddWithValue("@teacherID", Convert.ToInt32(teacher_combo.SelectedValue).ToString());
                           cmd.Parameters.AddWithValue("@subjid",  Convert.ToInt32(subject_combo.SelectedValue).ToString());
                           cmd.Parameters.AddWithValue("@i",i.ToString());
cmd.Parameters.AddWithValue("@ds",ds.Tables[0].Rows[k].ItemArray[0].ToString());
                            cmd.CommandText = q;
        cmd.Connection = myCon;
        myCon.Open();
        cmd.ExecuteNonQuery();
        System.Windows.Forms.MessageBox.Show("successfully added", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);}

推荐答案

您需要从参数声明中删除撇号.还要验证您要传递给查询的值中是否包含数据.在添加参数之前,还要分配您的CommandText.

You need to remove the apostrophes from your parameter declarations. Also verify that there is data in the values you are passing to the query. Also assign your CommandText before adding the parameters.

此外,在实现IDisposable的同时,也可以将OleDbCommand包裹在using语句中.

Also you can wrap the OleDbCommand in a using statement also as it implementes IDisposable.

然后,您尝试使用WHERE子句执行INSERT,该子句将不起作用.

Then, you are trying to do an INSERT with a WHERE clause, which will not work.

INSERT语句实际上是在表中插入"一行,您不能在已有行的地方插入一行.

INSERT statements are meant to actually "insert" a row into the table, you cannot insert a row where there is already a row.

您要查找的是UPDATE-我已经编辑了以下语法以体现这一点.

What you are looking for is UPDATE - I have edited the syntax below to reflect this.

using (OleDbConnection myCon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=timetabledata.accdb"))
using (OleDbCommand cmd = new OleDbCommand())
{                       
    cmd.CommandType = CommandType.Text;
    string q = "UPDATE timehourly SET teacheridh = @teacherId, SET subjectidh = @subjid  WHERE hour=@i AND dayid=@ds";
    cmd.CommandText = q;
    cmd.Parameters.AddWithValue("@teacherID", Convert.ToInt32(teacher_combo.SelectedValue).ToString());
    cmd.Parameters.AddWithValue("@subjid",  Convert.ToInt32(subject_combo.SelectedValue).ToString());
    cmd.Parameters.AddWithValue("@i",i.ToString());
    cmd.Parameters.AddWithValue("@ds",ds.Tables[0].Rows[k].ItemArray[0].ToString());

    cmd.Connection = myCon;
    myCon.Open();
    cmd.ExecuteNonQuery();
    System.Windows.Forms.MessageBox.Show("successfully added", "Caption",        MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
} 

如果您确实确实想做一个INSERT,那么只需从您的q字符串中取出WHERE.

If you really did mean to do an INSERT then just take the WHERE out of your q string.

这篇关于收到错误:查询输入必须至少包含一个表或查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆