当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

VB.Net - 数据库访问

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:59:14

名称和数据库名称。 单击测试连接按钮以检查连接是否成功。 在表单上添加一个DataGridView。 单击选择数据源组合框。 单击添加项目数据源链接。 这将打开“数据源配置向导”。 选择“数据库”作为数据源类型选择的DataSet作为数据库模型。 选择已设置的连接。 保存连接字符串。 在我们的示例中选择数据库对象Customers表,然后单击完成按钮。 选择“预览数据”链接以查看“结果”网格中的数据: 当使用Microsoft Visual Studio工具栏上的“开始”按钮运行应用程序时,将显示以下窗口: 示例2 在这个例子中,让我们使用代码访问DataGridView控件中的数据。 执行以下步骤: 在窗体中添加一个DataGridView控件和一个按钮。 将按钮控件的文本更改为“填充”。 双击按钮控件,为按钮的Click事件添加所需的代码,如下所示: Imports System.Data.SqlClientPublic Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load 'TODO: This line of code loads data into the 'TestDBDataSet.CUSTOMERS' table. You can move, or remove it, as needed. Me.CUSTOMERSTableAdapter.Fill(Me.TestDBDataSet.CUSTOMERS) ' Set the caption bar text of the form. Me.Text = "tutorialspoint.com" End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim connection As SqlConnection = New sqlconnection() connection.ConnectionString = "Data Source=KABIR-DESKTOP; _ Initial Catalog=testDB;Integrated Security=True" connection.Open() Dim adp As SqlDataAdapter = New SqlDataAdapter _ ("select * from Customers", connection) Dim ds As DataSet = New DataSet() adp.Fill(ds) DataGridView1.DataSource = ds.Tables(0) End SubEnd Class当使用Microsoft Visual Studio工具栏上的“开始”按钮执行并运行上述代码时,将显示以下窗口: 单击“填充”按钮可显示数据网格视图控件上的表: 创建表,列和行我们已经讨论过,像DataTable,DataColumn和DataRow这样的DataSet组件允许我们分别创建表,列和行。下面的例子演示了这个概念: 示例3 到目前为止,我们已经使用我们的计算机中已经存在的表和数据库。 在本示例中,我们将创建一个表,向其中添加列,行和数据,并使用DataGridView对象显示表。 执行以下步骤: 在窗体中添加一个DataGridView控件和一个按钮。 将按钮控件的文本更改为“填充”。 在代码编辑器中添加以下代码。 Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Set the caption bar text of the form. Me.Text = "tutorialspont.com" End Sub Private Function CreateDataSet() As DataSet 'creating a DataSet object for tables Dim dataset As DataSet = New DataSet() ' creating the student table Dim Students As DataTable = CreateStudentTable() dataset.Tables.Add(Students) Return dataset End Function Private Function CreateStudentTable() As DataTable Dim Students As DataTable Students = New DataTable("Student") ' adding columns AddNewColumn(Students, "System.Int32", "StudentID") AddNewColumn(Students, "System.String", "StudentName") AddNewColumn(Students, "System.String", "StudentCity") ' adding rows AddNewRow(Students, 1, "Zara Ali", "Kolkata") AddNewRow(Students, 2, "Shreya Sharma", "Delhi") AddNewRow(Students, 3, "Rini Mukherjee", "Hyderabad") AddNewRow(Students, 4, "Sunil Dubey", "Bikaner") AddNewRow(Students, 5, "Rajat Mishra", "Patna") Return Students End Function Private Sub AddNewColumn(ByRef table As DataTable, _ ByVal columnType As String, ByVal columnName As String) Dim column As DataColumn = _ table.Columns.Add(columnName, Type.GetType(columnType)) End Sub 'adding data into the table Private Sub AddNewRow(ByRef table As DataTable, ByRef id As Integer,_ ByRef name As String, ByRef city As String) Dim newrow As DataRow = table.NewRow() newrow("StudentID") = id newrow("StudentName") = name newrow("StudentCity") = city table.Rows.Add(newrow) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim ds As New DataSet ds = CreateDataSet() DataGridView1.DataSource = ds.Tables("Student") End SubEnd Class当使用Microsoft Visual Studio工具栏上的“开始”按钮执行并运行上述代码时,将显示以下窗口: 单击“填充”按钮可显示数据网格视图控件上的表:

上一页  [1] [2] 


VB.Net - 数据库访问