VS2010用VB语言实现数据库连接,并将表中的数据显示在GridView上。
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesBu...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dataset1 As New DataSet()
Dim conn As New SqlClient.SqlConnection
conn.ConnectionString = "Data Source=TCW-pc;initial catalog=master;Integrated Security=True"
''"data source=tcw-pc\sqlexpression;initial catalog='人';uid=master;providerName=System.Data.sqlClient"
Dim sqldataadapter As New SqlClient.SqlDataAdapter("select 李 from 人", conn)
sqldataadapter.Fill(dataset1, "s")
DataGridView1.DataSource = dataset1.Tables("s")
End Sub
代码是上面这样的,连接字符里面的字符串是复制连接成功后属性里显示的字符串,为什么没有预想中的显示数据到GridView呢?其实这段代码是抄录书上的一段代码
自己已经解决,谢谢大家 展开
Dim dataset1 As New DataSet()
Dim conn As New SqlClient.SqlConnection
conn.ConnectionString = "Data Source=TCW-pc;initial catalog=master;Integrated Security=True"
''"data source=tcw-pc\sqlexpression;initial catalog='人';uid=master;providerName=System.Data.sqlClient"
Dim sqldataadapter As New SqlClient.SqlDataAdapter("select 李 from 人", conn)
sqldataadapter.Fill(dataset1, "s")
DataGridView1.DataSource = dataset1.Tables("s")
End Sub
代码是上面这样的,连接字符里面的字符串是复制连接成功后属性里显示的字符串,为什么没有预想中的显示数据到GridView呢?其实这段代码是抄录书上的一段代码
自己已经解决,谢谢大家 展开
2个回答
展开全部
'================================ACCESS=================================
Public AccessConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\Simple.mdb;" & _
"Persist Security Info=False"
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
Dim AccessString As String = "SELECT * FROM 示例表 "
Dim AccessConn As New OleDb.OleDbConnection(AccessConnectionString)
AccessConn.Open()
Dim AccessAdapter As OleDbDataAdapter = New OleDbDataAdapter(AccessString, AccessConn)
'有返回值
Dim TempDataSet As New DataSet
AccessAdapter.Fill(TempDataSet)
DataGridView1.DataSource = TempDataSet.Tables(0)
AccessConn.Close()
Catch AccessException As Exception
MsgBox(AccessException.Message)
End Try
End Sub
陈恩点原创,转载请保留!
'================================SQL================================
Protected Const SqlConnectionString As String = _
"Server=CED-PC\SQLEXPRESS;" & _
"DataBase=;" & _
"Integrated Security=SSPI"
#Region "Display data"
' Handles the click event for the Display button. This handler gets the product
' information from the Contact table puts it into a DataSet which is used to
' bind to a DataGrid for display. Custom style objects are used to give the
' DataGrid a nice appearance.
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
If IsNothing(DataGridView1.DataSource) Then
Dim strSQL As String = _
"USE Simple" & vbCrLf & _
"SELECT * " & _
"FROM GetContacts"
Try
' The SqlConnection class allows you to communicate with SQL Server.
' The constructor accepts a connection string as an argument. This
' connection string uses Integrated Security, which means that you
' must have a login in SQL Server, or be part of the Administrators
' group for this to work.
Dim dbConnection As New SqlConnection(connectionString)
' A SqlCommand object is used to execute the SQL commands.
Dim cmd As New SqlCommand(strSQL, dbConnection)
' The SqlDataAdapter is responsible for using a SqlCommand object to
' fill a DataSet.
Dim da As New SqlDataAdapter(cmd)
Dim dsContacts As New DataSet()
da.Fill(dsContacts, "Contact")
With Me.DataGridView1
.Visible = True
.AutoGenerateColumns = False
.AlternatingRowsDefaultCellStyle.BackColor = Color.Lavender
.BackColor = Color.WhiteSmoke
.ForeColor = Color.MidnightBlue
.CellBorderStyle = DataGridViewCellBorderStyle.None
.ColumnHeadersDefaultCellStyle.Font = New Font("Tahoma", 8.0!, FontStyle.Bold)
.ColumnHeadersDefaultCellStyle.BackColor = Color.MidnightBlue
.ColumnHeadersDefaultCellStyle.ForeColor = Color.WhiteSmoke
.DefaultCellStyle.ForeColor = Color.MidnightBlue
.DefaultCellStyle.BackColor = Color.WhiteSmoke
End With
Me.DataGridView1.DataSource = dsContacts.Tables(0)
Dim newColumn As Integer = Me.DataGridView1.Columns.Add("ContactID", "Contact ID")
Me.DataGridView1.Columns(newColumn).DataPropertyName = "ContactID"
newColumn = Me.DataGridView1.Columns.Add("FirstName", "First Name")
Me.DataGridView1.Columns(newColumn).DataPropertyName = "FirstName"
newColumn = Me.DataGridView1.Columns.Add("LastName", "Last Name")
Me.DataGridView1.Columns(newColumn).DataPropertyName = "LastName"
Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
#End Region
陈恩点原创,转载请保留!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询