2 qq 36805380 qq_36805380 于 2017.01.11 09:21 提问

vb.net问题,这句话拆解开来是什么意思? (下面第一句话)

Dim mycon222 = New OleDbCommand(sqlstr1, mycon111)

其中mycon111已定义为New OleDbConnection,sqlstr1为string。请教大神解释一下这句话拆开来是什么意思
能不能代码写一下?我只知道有dim mycon222 as new oledbcommand
然后之后怎么写?

3个回答

haflxzc
haflxzc   2017.01.11 16:35
已采纳

连接数据库吗?下面函数是我自己写的vb.net查询数据库函数:
sql传递SQL语句,dt返回查询结果;当查询出错时函数返回False,err保存错误信息
Public Function selectbd(ByVal sql As String, ByVal conn As String, ByRef dt As DataTable, ByVal err As String) As Boolean
Try
Dim objconn As New OledbConnection(conn)
Dim objcomm As New OledbCommand
Dim objda As New OledbDataAdapter
Dim objdt As New DataTable
objcomm.CommandText = sql
objcomm.CommandType = CommandType.Text
objcomm.Connection = objconn
objda.SelectCommand = objcomm
objconn.Open()
objda.Fill(objdt)
dt = objdt
Return True
objconn.Close()
Catch ex As Exception
err = ex.Message
Return False
End Try

End Function
tang_cheng
tang_cheng   2017.01.11 09:31

就是执行在mycon111指定的数据库内执行sqlstr1内包含的SQL文。查询结果用ExecuteReader();可以取出。示例代码如下:

        OleDbDataReader reader = mycon222.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader(0).ToString());
        }
haflxzc
haflxzc   2017.01.11 16:36

漏了conn是数据库连接串

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!