向VB代码添加SQL查询时编译错误

时间:2017-03-21 03:46:24

标签: sql vb.net

为什么每次将SQL查询代码放入VB.NET中时都会出错?

这是我的代码:

Imports System.Data.OleDb
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles                                                                   MyBase.Load
        Call connection()
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim da As New OleDbDataAdapter
        ds.Tables.Add(dt)
        da = New OleDbDataAdapter("SELECT T_Order.Order_ID, T_Order.Customer_ID, T_Customer.Fname, T_Customer.Mname, T_Customer.Lname, T_Order.Employee_id, T_Employee.Employee_name
        FROM ( INNER JOIN ((T_Employee INNER JOIN (T_Customer INNER JOIN T_Order ON     T_Customer.Customer_ID = T_Order.Customer_ID) ON T_Employee.Employee_ID = T_Order.Employee_id) INNER JOIN T_OrderDetail ON T_Order.Order_ID = T_OrderDetail.Order_ID) ON T_Product.Product_Code = T_OrderDetail.Product_Code", cn)
        da.Fill(dt)
        DataGridView1.DataSource = dt.DefaultView

这里是错误输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该将这些错误2行分为1行,如下所示

da = New OleDbDataAdapter("SELECT T_Order.Order_ID, T_Order.Customer_ID, T_Customer.Fname, T_Customer.Mname, T_Customer.Lname, T_Order.Employee_id, T_Employee.Employee_name FROM ( INNER JOIN ((T_Employee INNER JOIN (T_Customer INNER JOIN T_Order ON     T_Customer.Customer_ID = T_Order.Customer_ID) ON T_Employee.Employee_ID = T_Order.Employee_id) INNER JOIN T_OrderDetail ON T_Order.Order_ID = T_OrderDetail.Order_ID) ON T_Product.Product_Code = T_OrderDetail.Product_Code", cn)

在VB中不能指定像C这样的多行字符串。 如果你需要VB中的多行字符串,你应该这样做

str = "string1" & _ 
      "string2" & _
      "string3"

因此,您的代码如下所示。

da = New OleDbDataAdapter("SELECT T_Order.Order_ID, T_Order.Customer_ID, T_Customer.Fname, T_Customer.Mname, T_Customer.Lname, T_Order.Employee_id, T_Employee.Employee_name" & _
"FROM ( INNER JOIN ((T_Employee INNER JOIN (T_Customer INNER JOIN T_Order ON     T_Customer.Customer_ID = T_Order.Customer_ID) ON T_Employee.Employee_ID = T_Order.Employee_id) INNER JOIN T_OrderDetail ON T_Order.Order_ID = T_OrderDetail.Order_ID) ON T_Product.Product_Code = T_OrderDetail.Product_Code", cn)