保存以访问数据库 [英] To save to access database

查看:60
本文介绍了保存以访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 当我单击提交按钮以保存到数据库时,出现错误"ConnectionString属性尚未初始化",并带有箭头,该行指向具有此代码"cntest.Open()"的行用过的如下所示

Hi When i click the submit button to save to the database then i have the error ''The ConnectionString property has not been initialized'' with an arrow pointing to a line which have this code ''cntest.Open()'' the codes i have used are shown below

Imports System.Data.OleDb
Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim datest As New OleDbDataAdapter
        Dim dstest As New DataSet
        Dim cntest As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Sammy\Desktop\test\test.accdb")


    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        Dim cntest As New OleDbConnection
        cntest = New OleDbConnection

        cntest.Open()
        Dim dstest As New DataSet
        Dim dt As DataTable = dstest.Tables("test")
        Dim newrow As DataRow
        newrow = dt.NewRow()
        ''add a row
        dt.Rows.Add(newrow)
        newrow("StudentName") = txtStudentName.Text
        newrow("Faculty") = txtFaculty.Text
        newrow("Comment") = txtComment.Text

    End Sub

推荐答案

那是正确的.该方法尚未初始化连接.您已经在PageLoad中完成了此操作,但是,由于变量仅在本地作用域内,因此它将在PageLoad方法退出后进行处理.基本的东西.
That is correct. The connection has not been initialized in the method. You have done so in the PageLoad, however, since the variable is only scoped locally it will be disposed after PageLoad method exits. Basic stuff.


这篇关于保存以访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆