调用.mdf数据库文件的实例
分类:VB.Net
乐博网lob.cn提示:调试环境为 vs2005 + windows2003 / windows2008 / xp / vista + .NET Framework 2.0
代码如下:
Option Strict On Imports System.Data.SqlClient
Public Class LOB Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click ' 利用 SqlConnectionStringBuilder 对象来构建连接字符串。 Dim connectStringBuilder As New SqlConnectionStringBuilder() connectStringBuilder.DataSource = "(local)\SQLEXPRESS" ' 使用相对路径的手法来指定所要连结的 SQL Server 数据库文件。 connectStringBuilder.AttachDBFilename = "|DataDirectory|\App_Database\乐博网.mdf" connectStringBuilder.IntegratedSecurity = True ' 只连结数据库而不附加数据库。 connectStringBuilder.UserInstance = True
Try Using cn As New SqlConnection(connectStringBuilder.ConnectionString) cn.Open()
Dim myCMD As New SqlCommand("SELECT ProductPhotoID,ThumbNailPhoto,LargePhoto FROM Production.ProductPhoto", cn)
Using myReader As SqlDataReader = myCMD.ExecuteReader()
' 将 BindingSource 组件系结至 SqlDataReader。 Me.BindingSource1.DataSource = myReader
' 将 DataGridView 控件系结至 BindingSource 组件。 Me.DataGridView1.DataSource = Me.BindingSource1
End Using End Using '更多.net源码和教程,来自[乐博网 www.lob.cn]
Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub End Class
|