【问题标题】:Syntax error in retrieving data in c#.net在 c#.net 中检索数据时出现语法错误
【发布时间】:2014-02-06 10:28:30
【问题描述】:
OleDbConnection con = new OleDbConnection(constr);
con.Open();

OleDbCommand Comm1 = new OleDbCommand("select customer_id from tb_customer ORDER BY customer_id desc limit 1", con);

OleDbDataReader DR_customer = Comm1.ExecuteReader();
if (DR_customer.Read())
{
     textBox1.Text = DR_customer.GetValue(0).ToString();
}

在这里,我试图获取最后输入的数据的 customer_id。但我在 ORDER By 子句中遇到语法错误。

【问题讨论】:

  • 不应该是select top 1 customer_id而不是limit 1吗?限制是 MySQL 运算符
  • @user 查询限制后 1 是什么?
  • 但我想要最后一个 id.??
  • 你想连接什么数据库引擎?
  • 如果 OP 使用的是 mySQL 怎么办?

标签: c#


【解决方案1】:

你能试试这个吗:

OleDbConnection con = new OleDbConnection(constr);
con.Open();
OleDbCommand Comm1 = new OleDbCommand("select top 1  customer_id from tb_customer ORDER BY customer_id desc", con);
OleDbDataReader DR_customer = Comm1.ExecuteReader();
if (DR_customer.Read())
   textBox1.Text = DR_customer.GetValue(0).ToString();

【讨论】:

  • 我想要这个值在一个变量中而不是一个文本框并将它插入到数据库中......那么如何获取数据??
【解决方案2】:

试试这个。 top 1

select top 1 customer_id from tb_customer ORDER BY customer_id desc 

【讨论】:

    【解决方案3】:

    您可能需要top 1 而不是limit 1

    new OleDbCommand("select top 1 customer_id from tb_customer ORDER BY customer_id desc", con);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2020-05-18
      • 2013-07-15
      相关资源
      最近更新 更多