Microsoft.SqlServerCe.Client vs System.Data.SqlServerCe - vb.net

I am writing a vb application that uses tableadapters.
I want to use the connection property of the tableadapters.
I have the following code:
Imports System.Data.SqlServerCe
Dim tbladaptBirds as TestBirdDBDataSetTableAdapters.tblBirdsTableAdapter
Dim tbladptrVetLinks As New TestBirdDBDataSetTableAdapters.tblVetLinksTableAdapter
Dim tbladptrVets As New TestBirdDBDataSetTableAdapters.tblVetsTableAdapter
Dim con As New SqlCeConnection
tbladaptBirds.Connection = con
This gives me the following error:
Value of type 'System.Data.SqlServerCe.SqlCeConnection' cannot be converted to 'Microsoft.SqlServerCe.Client.SqlCeConnection'.
When I replaced Imports System.Data.SqlServerCe with Imports Microsoft.SqlServerCe.Client, the error went away.
I was just playing around with a new project, and I typed in the following code
Imports System.Data.SqlServerCe
Dim con As New SqlCeConnection
Dim tbladptr As New DatasetTestDataSetTableAdapters.tblInfoTableAdapter
tbladptr.Connection = con ' no error
Why do I need Imports Microsoft.SqlServerCe.Client in the application I am writing while Imports System.Data.SqlServerCe is what I need in the application I was playing around with?
Thanks

Related

Visual Basic Built in database Connection String Syntax Error

I am using the built-in database for my program. When I try to put in the connection string, VB cannot detect the connection string and shows a syntax error on line 7 after the new SqlConnection. I am sure that I copied the complete connection string from the properties page.
I read this post but it seems to be a different question. Below is my code for the connection. Is there any mistake in my code? Thanks for all the help!
Imports System.Data.SqlClient
Public Class Login
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim da As SqlDataAdapter
Dim sql As String
Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf";Integrated Security=True")
That'll obviously show you a syntax error, look at your following line:
"C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"
Replace the double-quotes to ""<abc>"" to get like "<abc>" on execution because you've already used "<abc>" in New SqlConnection("...").
Rather than:
Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf";Integrated Security=True")
you should have:
Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"";Integrated Security=True")

Connect to a database with visual basic

I am trying to connect an database, but it crash and it say that there are a
Connection problem, i have tryed this code in other pc, but now does not work
the error is:
There was a network or instance-specific error while trying to establish a connection to SQL Server. The server was not found or is not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
the code is this:
Module Module1
'FUNZIONE PER LA CONNESSIONE AL DATABASE
Public Function Connetti(ByVal sql As String, ByVal namedset As String, ByRef dataSet As DataSet)
Dim myConnString As String = "Persist Security Info=False;database=test;server=95.134.229.235;user id=web;pwd=fiautoppzione" 'server=Server206 'user id=utente_std;pwd=145111
Dim myConnection As New SqlConnection(myConnString)
Dim myInsertQuery As String = sql
Dim myCommand As New SqlCommand(myInsertQuery)
Dim myada As New SqlDataAdapter
Dim mydset As New DataSet(namedset)
Dim mydbs As New BindingSource
'IO.File.AppendAllText("C:\aggiorna.txt", sql & vbCrLf)
myCommand.Connection = myConnection
myada.SelectCommand = myCommand
myada.MissingSchemaAction = MissingSchemaAction.AddWithKey
mydset.EnforceConstraints = False
mydset.Clear()
myada.Fill(mydset, namedset)
mydbs.DataSource = mydset.Tables(namedset)
If Mid(sql, 1, 6) = "update" Then
mydset.Clear()
myada.Fill(mydset, namedset)
mydbs.DataSource = mydset.Tables(namedset)
End If
myConnection.Open()
dataSet = mydset
myCommand.Connection.Close()
Return mydbs
End Function
and the first part of the code:
Option Explicit On
Imports System.Data
Imports System.Data.Odbc
Imports System.Xml.Xsl
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Imports FIAppStabilimento.MySql.Data
Imports System.Data.SqlClient
Namespace MySql.Data.MySqlClient
End Namespace
For mySQL connection, you must not use the sqldataconnection.
First download MySql Connector/Net
Then add the following reference:
MySQL.Data
Sometimes MySql for somehow is not listed under .NET tab. Go to Browse tab, and navigate to the following path:
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.6.4\Assemblies\v2.0
or C:\Program Files (x86)\MySQL\MySQL Connector Net 6.6.4\Assemblies\v4.0
and add MySql.Data.dll
Add the following code before Public Class Form1
MYSQL.dll Reference Image
Imports MySql.Data.MySqlClient
Then add the following declaration below Public Class Form1
Dim conn As New MySqlConnection
Public Sub connect()
Dim DatabaseName As String = "test"
Dim server As String = "95.134.229.235"
Dim userName As String = "web"
Dim password As String = "fiautoppzione"
If Not conn Is Nothing Then conn.Close()
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
Try
conn.Open()
MsgBox("Connected")
Catch ex As Exception
MsgBox(ex.Message)
End Try
conn.Close()
End Sub

Linking VB to MS Accsss Database

I am trying to link a VB application with a MS access database.
The only way I seem able to do this is through adding a data source. Is there a way or doing this so you use it as an external and code to the file location rather than actually uploading the database to vb?
Thanks
Imports System.Data.Oledb
Dim con As New OledbConnection("Provider=microsoft.Jet.oledb.4.0DataSource=D:\mydata.mdb;")
Dim cmd As New OledbCommand
Public Sub New()
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT * FROM table1"
End Sub

Declaring a connection string in console application

I am creating a console app and I have a connection string in the app.config file. When I try to reference this in my code I get an error on the configuration manager (ie its underlined).
Public Function getTPLvalue() As String
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("GasNominationsConnectionString").ConnectionString)
conn.Open()
Dim cmd As New SqlCommand("select tpl from JADhist where dateSubmitted = (select MAX(datesubmitted) from JADhist)", conn)
Dim TPL As String = (cmd.ExecuteScalar())
conn.Close()
Return TPL
End Function
any ideas
You have to reference the namespace System.Configuration using:
Imports System.Configuration;

vb.net create connection class

I want to create a class in vb.net that make the connection to database.
Also I need the way to use this class inside forms.
I need a code example of both the class and the form call
Thanks.
Right Click on your project in solution explorer > Add > Class then right the following. Change as per your requirements.
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbPermission
Imports System.Math
Public Class ClassCon
Dim Filepath As String = Application.StartupPath & "\yourDatabase.mdb;"
Public constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Jet OLEDB:Database Password=yourPassword;data source=" & Filepath
Public con As New OleDbConnection(constring)
Now use the connection class in form as below
Imports System.Data.OleDb
Public Class form1
Dim cmd As New OleDbCommand
Dim objcon As New ClassCon
If (objcon.con.State = ConnectionState.Closed) Then objcon.con.Open()
cmd100 = New OleDbCommand("INSERT INTO Mosey VALUES('" & TextBox1.Text & "')", objcon.con)
cmd.ExecuteNonQuery()
objcon.con.Close()
I am not an expert but my suggestion is you should, rather you must know what you are trying to search in Google. I am always able to find what I am looking for. And if you are still not sure then post here with your code i.e what you have tried so far.
All these code above are not tested with IDE so there may be typo.