2

谁能告诉我我在这里做错了什么?非常基本的 Visual Basic 经验水平。

我试图从提供的数据库中检索 ORDERID、ORDERDATE 和 CUSTOMERID 并在 dataGridView 中显示它们?

Dim con1 As New OleDbConnection
con1.ConnectionString = _
"Provider=msdaora;Data Source=orabis;User Id=112221800;Password=112221800;"

'Dim con1 As New SqlConnection("Provider=msdaora;Data Source=orabis;User Id=112221800;Password=112221800;")
con1.Open()
Dim cmd1 As New SqlCommand("select ORDERID, ORDERDATE AND CUSTOMERID from CUSORDER", con1)

Dim ada1 As New SqlDataAdapter(cmd1)
Dim ds1 As New DataSet
ada1.Fill(ds1)
ViewDeliverys.DataGridView1.DataSource = ds1.Tables(0)

con1.Close()
4

2 回答 2

1

您不能使用 a OleDbConnectionwhith a SqlCommand(SQL-Server)。您使用的是哪个 rdbms?

这适用于 SQL-Server:

Using con = New SqlConnection(connectionString)
    Using da = New SqlDataAdapter("SELECT ORDERID, ORDERDATE AND CUSTOMERID from CUSORDER", con)
        Dim table = New DataTable()
        da.Fill(table)
        ViewDeliverys.DataGridView1.DataSource = table
    End Using
End Using

编辑:这是OleDb版本:

Using con = New OleDbConnection(connectionString)
    Using da = New OleDbDataAdapter("SELECT ORDERID, ORDERDATE AND CUSTOMERID from CUSORDER", con)
        Dim table = New DataTable()
        da.Fill(table)
        ViewDeliverys.DataGridView1.DataSource = table
    End Using
End Using
于 2013-03-07T10:39:49.977 回答
0

您使用 SQL Sever 或 MS Access 什么数据库?如果您使用 SQL Server,请使用 SQL Connection,然后在 MS Access 上使用 OleDBConnection。

对于正确的连接字符串,请检查:

http://www.connectionstrings.com

还可以从以下位置查看本教程:

http://vb.net-informations.com/ado.net-dataproviders/ado.net-sqldataadapter

并且还从以下位置替换您的查询:

Dim cmd1 As New SqlCommand("select ORDERID, ORDERDATE AND CUSTOMERID from CUSORDER", con1)

哪里ORDERDATE AND CUSTOMERID会给你一个错误

将其更改为:

Dim cmd1 As New SqlCommand("select ORDERID, ORDERDATE,CUSTOMERID from CUSORDER", con1)
于 2013-03-07T10:40:07.840 回答