Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 16093

VS 2019 ADO.NET Can't Open Database File

$
0
0
Hey guys, long time no see. I decided to play around with SQL using VB.Net with ADO.Net, and for some reason I just can't seem to connect to the database file. I used Tools > Connect to Database, and linked the file with that, which is an accdb file from Access. Didn't change anything really afterwards. And then I ran this code:

vb.net Code:
  1. Option Explicit On
  2. Option Strict On
  3. Imports System.Data.SqlClient
  4.  
  5. Public Class Form1
  6.     Private myConn As SqlConnection
  7.     Private myCmd As SqlCommand
  8.     Private myReader As SqlDataReader
  9.     Private results As String
  10.  
  11.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  12.         myConn = New SqlConnection("Data Source = Database1")
  13.  
  14.         'Open the connection.
  15.         myConn.Open()
  16.     End Sub
  17.  
  18.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  19.         'Create a Command object.
  20.         myCmd = myConn.CreateCommand
  21.         myCmd.CommandText = "SELECT * FROM Table1 WHERE ID = " & TextBox1.Text
  22.         myReader = myCmd.ExecuteReader()
  23.         results = results & myReader.GetString(0) & vbTab & myReader.GetString(1)
  24.         Label1.Text = results
  25.     End Sub
  26. End Class

As a result, I get an unhandled exception. I even tried using an mdb file, and it still fails. Even tried adding "Initial Catalog=Database1;Integrated Security=True" for the myConn connection, and nothing worked. Anyone got any bright ideas? Thanks in advance. :bigyello:

Viewing all articles
Browse latest Browse all 16093

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>