高手请帮小弟解决下,vb.net2005 datagridview连接数据库
高手请帮小弟解决下,vb.net2005 datagridview连接数据库,本人刚学,还请高手们指点。 --------------------编程问答-------------------- '将数据库DB.mdb放在Debug目录下:Imports System.Data.OleDb
Public Class Form1
Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\DB.mdb")
Dim MyCommand As OleDbCommand
Dim dbset As New DataSet
Dim dataA As OleDbDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyConnection.Open()
MyCommand = New OleDbCommand("Select * from BOOK ", MyConnection)
dataA = New OleDbDataAdapter(MyCommand)
dataA.Fill(dbset, "BOOK")
DataGridView1.DataSource = dbset.Tables("BOOK")
MyConnection.Close()
End Sub
end class --------------------编程问答-------------------- 嗯,可以用代码,就是上面这位写的。也可以自己拖个datagridview。然后配置它的datasource.这是根据配置界面可以完成。。 --------------------编程问答-------------------- Using cn As New SqlConnection("")
cn.Open()
Dim ds As New DataSet()
Dim cmd As New SqlCommand("select * from Tb", cn)
Dim da As New SqlDataAdapter(cmd)
Dim builder As New SqlCommandBuilder(da)
da.Fill(ds, "Test")
Me.dataGridView1.DataSource = ds.Tables(0).DefaultView
End Using --------------------编程问答-------------------- 大家都给了你比较完整的代码,其实简单的就是分为二个部分,一部分将数据读取出来放到一个dataTable中,第二部分就是直接绑定至gridview上。 --------------------编程问答-------------------- 学习ing --------------------编程问答-------------------- 每天回帖即可获得10分可用分! --------------------编程问答--------------------
学习+关注! --------------------编程问答-------------------- 学习中。。。。。。。。 --------------------编程问答-------------------- SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select * from film_detail order by filmName ";
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
gridview.DataSource = dt;
gridview.DataBind();
dr.Close();
conn.Close();
这个一定好使,我刚用完. --------------------编程问答-------------------- VB.NET中的代码: 试试吧!
Imports system.data
imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim con as new sqlconnection("server=(local);uid=sa,pwd=sa;database=pubs")
dim cad as new sqldataadapter("select * from authors",con)
dim bset as new dataset
cad.fill(bset,"authors")
me.datagridview1.datasource=bset.Tables("authors").DefaultView
bset.Dispose()
cad.Dispose()
End Sub
end Class --------------------编程问答-------------------- 其实你该先看书。。。。。 --------------------编程问答-------------------- ExcelWord正解 --------------------编程问答-------------------- 接份,就不多说 --------------------编程问答-------------------- 学习... --------------------编程问答-------------------- 给的源码貌似都能正常运行的。 --------------------编程问答-------------------- 来看一下 --------------------编程问答-------------------- xuexi
--------------------编程问答-------------------- 正在寻找好方法!! --------------------编程问答-------------------- 我自己写的连接数据库的dll
这个是oledb
'************************************************操作OLE类型数据********************************************************
'* *
'* 函数: 1.GetTable:传递一个SQL语句,以数据表的形式返回查询结果 *
'* 2.GetValue:传递一个SQL语句,返回查询结果的第一个字段值 *
'* 3.IsExists:根据提供的SQL语句判断该数值是否存在,返回结果为布尔型 *
'* 4.UpdateTable:根据提供的SQL语句对指定的数据表记录进行插入,更新和删除,成功返回True,失败返回False *
'* 5.BatchUpdateTable:根据提供的SQL语句数组,对数据库的表记录进行批量操作 *
'* *
'***********************************************************************************************************************
Imports System.Data.OleDb
Public Enum TranType
Access = 0
Excel = 1
DBF = 2
End Enum
Public Class OLE
#Region "声明"
Private oleconn As OleDbConnection
Private oleadapter As OleDbDataAdapter
Private olecommand As OleDbCommand
Private stroleconn As String = ""
#End Region
#Region "方法和函数"
Sub New(ByVal DataType As TranType, ByVal SourcePath As String, Optional ByVal PassWd As String = "")
Select Case DataType
Case TranType.Excel _
: stroleconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & SourcePath & _
";Extended Properties='Excel 8.0'"
Case TranType.Access _
: stroleconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & SourcePath & _
";jet oledb:database Password=" & PassWd
Case TranType.DBF _
: stroleconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & SourcePath & _
";Extended Properties=dBASE IV;User ID=Admin;Password="
End Select
End Sub
'GetTable
Public Function GetTable(ByVal strSql As String) As DataTable
Try
Dim tmptb As New DataTable
oleconn = New OleDb.OleDbConnection(stroleconn)
oleconn.Open()
oleadapter = New OleDbDataAdapter(strSql, oleconn)
oleadapter.Fill(tmptb)
oleadapter.Dispose()
oleconn.Close()
Return tmptb
Catch ex As Exception
oleadapter.Dispose()
oleconn.Close()
Throw ex
Return Nothing
End Try
End Function
'GetValue
Public Function GetValue(ByVal strSql As String) As String
Try
Dim tmptb As New DataTable
oleconn = New OleDb.OleDbConnection(stroleconn)
oleconn.Open()
oleadapter = New OleDbDataAdapter(strSql, oleconn)
oleadapter.Fill(tmptb)
oleadapter.Dispose()
oleconn.Close()
If Not tmptb.Rows.Count < 1 Then
Return tmptb.Rows(0).Item(0).ToString.Trim
Else
Return ""
End If
Catch ex As Exception
oleadapter.Dispose()
oleconn.Close()
Throw ex
Return ""
End Try
End Function
'IsExists
Public Function IsExists(ByVal strSql As String) As Boolean
Try
Dim tmptb As New DataTable
oleconn = New OleDb.OleDbConnection(stroleconn)
oleconn.Open()
oleadapter = New OleDbDataAdapter(strSql, oleconn)
oleadapter.Fill(tmptb)
oleadapter.Dispose()
oleconn.Close()
If tmptb.Rows.Count < 1 Then
Return False
Else
Return True
End If
Catch ex As Exception
oleadapter.Dispose()
oleconn.Close()
Throw ex
Return False
End Try
End Function
'UpdateTable
Public Function UpdateTable(ByVal strSql As String) As Boolean
Try
oleconn = New OleDb.OleDbConnection(stroleconn)
oleconn.Open()
olecommand = New OleDbCommand(strSql, oleconn)
olecommand.ExecuteNonQuery()
olecommand.Dispose()
oleconn.Close()
Return True
Catch ex As Exception
olecommand.Dispose()
oleconn.Close()
Throw ex
Return False
End Try
End Function
'BatchUpdateTable
Public Function BatchUpdateTable(ByVal strSql() As String) As Boolean
Dim Trans As OleDbTransaction = Nothing
Try
oleconn = New OleDb.OleDbConnection(stroleconn)
oleconn.Open()
olecommand = New OleDbCommand
olecommand.CommandType = CommandType.Text
olecommand.Connection = oleconn
Trans = oleconn.BeginTransaction
olecommand.Transaction = Trans
For i As Integer = 0 To strSql.Length - 1
If Not String.IsNullOrEmpty(strSql(i)) Then
olecommand.CommandText = strSql(i).ToString
olecommand.ExecuteNonQuery()
End If
Next
Trans.Commit()
Trans.Dispose()
olecommand.Dispose()
oleconn.Close()
Return True
Catch ex As Exception
Trans.Rollback()
Trans.Dispose()
olecommand.Dispose()
oleconn.Close()
Throw ex
Return False
End Try
End Function
#End Region
End Class
--------------------编程问答-------------------- 这个是SQL
--------------------编程问答-------------------- 向牛X 人 学习!! --------------------编程问答-------------------- 建议楼主在百度搜索“datagridview的使用教程”,多看点基础方面的书,才能用得得手应手
'********************************************与SQL服务器交互数据********************************************************
'* *
'* 函数: 1.GetTable:传递一个SQL语句,以数据表的形式返回查询结果 *
'* 2.GetValue:传递一个SQL语句,返回查询结果的第一个字段值 *
'* 3.IsExists:根据提供的SQL语句判断该数值是否存在,返回结果为布尔型 *
'* 4.UpdateTable:根据提供的SQL语句对指定的数据表记录进行插入,更新和删除,成功返回True,失败返回False *
'* 5.BatchUpdateTable:根据提供的SQL语句数组,对数据库的表记录进行批量操作 *
'* *
'***********************************************************************************************************************
Imports System.Data.SqlClient
Public Class SQL
#Region "声明"
Dim Conn As SqlConnection
Dim Adapter As SqlDataAdapter
Dim Command As SqlCommand
Private strConnect As String = ""
Sub New(ByVal SrvName As String, ByVal DataBaseName As String, ByVal UserID As String, ByVal PassWd As String, _
Optional ByVal Cnt As Integer = 30)
strConnect = "Server=" & SrvName & ";database=" & DataBaseName & ";user id=" & UserID & _
";pwd=" & PassWd & ";Connect Timeout=" & Cnt.ToString
Try
Conn = New SqlConnection(strConnect)
Catch ex As Exception
Throw ex
Return
End Try
End Sub
#End Region
#Region "方法和函数"
'GetTable
Public Function GetTable(ByVal strSql As String) As DataTable
Try
Dim tmptb As New DataTable
Conn = New SqlConnection(strConnect)
Conn.Open()
Adapter = New SqlDataAdapter(strSql, Conn)
Adapter.Fill(tmptb)
Adapter.Dispose()
Conn.Dispose()
Return tmptb
Catch ex As Exception
Adapter.Dispose()
Conn.Close()
Throw ex
Return Nothing
End Try
End Function
'GetValue
Public Function GetValue(ByVal strSql As String) As String
Try
Dim tmptb As New DataTable
Conn = New SqlConnection(strConnect)
Conn.Open()
Adapter = New SqlDataAdapter(strSql, Conn)
Adapter.Fill(tmptb)
Adapter.Dispose()
Conn.Close()
Return tmptb.Rows(0).Item(0).ToString.Trim
Catch ex As Exception
Adapter.Dispose()
Conn.Close()
Throw ex
Return ""
End Try
End Function
'IsExists
Public Function IsExists(ByVal strSql As String) As Boolean
Try
Dim tmptb As New DataTable
Conn = New SqlConnection(strConnect)
Conn.Open()
Adapter = New SqlDataAdapter(strSql, Conn)
Adapter.Fill(tmptb)
Adapter.Dispose()
Conn.Close()
If tmptb.Rows.Count < 1 Then
Return False
Else
Return True
End If
Catch ex As Exception
Adapter.Dispose()
Conn.Close()
Throw ex
Return False
End Try
End Function
'UpdateTable
Public Function UpdateTable(ByVal strSql As String) As Boolean
Try
Conn = New SqlConnection(strConnect)
Command = New SqlCommand(strSql, Conn)
Command.ExecuteNonQuery()
Command.Dispose()
Conn.Close()
Return True
Catch ex As Exception
Command.Dispose()
Conn.Close()
Throw ex
Return False
End Try
End Function
'BatchUpdateTable
Public Function BatchUpdateTable(ByVal strSql() As String) As Boolean
Dim SqlTran As SqlTransaction = Nothing
Try
Conn = New SqlConnection(strConnect)
Command = New SqlCommand
Command.Connection = Conn
Command.CommandType = CommandType.Text
SqlTran = Conn.BeginTransaction
Command.Transaction = SqlTran
For i As Integer = 0 To strSql.Length - 1
If Not String.IsNullOrEmpty(strSql(i)) Then
Command.CommandText = strSql(i).ToString
Command.ExecuteNonQuery()
End If
Next
SqlTran.Commit()
Command.Dispose()
SqlTran.Dispose()
Conn.Close()
Return True
Catch ex As Exception
SqlTran.Rollback()
Command.Dispose()
SqlTran.Dispose()
Conn.Close()
Throw ex
Return False
End Try
End Function
#End Region
End Class
补充:.NET技术 , VB.NET