FROM子句中的语法错误 - SQL错误

时间:2015-04-29 08:20:42

标签: sql syntax

我尝试过以前的帖子中的很多变化。有谁知道这可能是什么原因造成的?

(我知道代码不应该从安全的角度来使用,但它是一个小型的基于本地的系统)

Con.Open();
oleDbCmd.Connection = Con;

OleDbDataReader rdr = null;
OleDbCommand cmd = new OleDbCommand("select * from Customers", Con);
rdr = cmd.ExecuteReader();

while (rdr.Read())
{
                Int32 intChech = Convert.ToInt32(cboCustomerID.Text);
                if (intChech == (Int32)rdr.GetValue(0))
                {
                    txtTitle.Text = (string)rdr.GetValue(1);
                    txtSurname.Text = (string)rdr.GetValue(2);
                    txtForename.Text = (string)rdr.GetValue(3);
                    txtAddress.Text = (string)rdr.GetValue(4);
                    txtTown.Text = (string)rdr.GetValue(5);
                    txtCounty.Text = (string)rdr.GetValue(6);
                    txtPostCode.Text = (string)rdr.GetValue(7);
                    txtTelephone.Text = (string)rdr.GetValue(8);
                }
            }
            //PULL DATABASE INFORMATION FOR CAR INFO
           string strSearch = Convert.ToString(cboCustomerID.Text);
           string strSQL = @"select * from Hire if [Customer ID] = '"; 


           OleDbDataAdapter dAdapter1 = new OleDbDataAdapter(strSQL, Con);
           OleDbCommandBuilder cBuidler1 = new OleDbCommandBuilder(dAdapter1);

            DataTable dataTable1 = new DataTable();
            DataSet ds1 = new DataSet();

            dAdapter1.Fill(dataTable1);
            for (int i = 0; i < dataTable1.Rows.Count; i++)

            {
                dgHire1.Rows.Add(dataTable1.Rows[i][0], dataTable1.Rows[i][1], dataTable1.Rows[i][2], 
                dataTable1.Rows[i][3], dataTable1.Rows[i][4]);
            }
}

1 个答案:

答案 0 :(得分:2)

您应该使用以下代码:

//PULL DATABASE INFORMATION FOR CAR INFO
string strSQL = @"select * from Hire where [Customer ID] = ?";

OleDbDataAdapter dAdapter1 = new OleDbDataAdapter(strSQL, Con);
dAdapter1.SelectCommand.Parameters.Add("[Customer ID]", cboCustomerID.Text);
OleDbCommandBuilder cBuidler1 = new OleDbCommandBuilder(dAdapter1);

或者如果您不想使用命令参数,可以使用:

string strSQL = @"select * from Hire where [Customer ID] = '" + cboCustomerID.Text + "'";
OleDbDataAdapter dAdapter1 = new OleDbDataAdapter(strSQL, Con);
OleDbCommandBuilder cBuidler1 = new OleDbCommandBuilder(dAdapter1);

但这是不好的做法。