Can't access Local SQL database manually by connection string in VB.NET using Visual Studio - sql

I have a project set up with 3 separate tables. I used the built-in designer from Visual Studio.
The tables contain
Students
Courses
Tests in each course
All the tables share a studentnumber.
I have no problem getting and accessing the data true different tableadapters and corresponding bindingsources automatically generated by Visual Studio when I "drag and drop" them on the forms. I also am able to use the query builder to get the data I need from each table.
My problem occurs when I try to access the database manually via connection string.
I get this error:
Cannot attach file local path\HovedDatabase.mdf as database Outcomes because the file is already in use for database local path\HovedDatabase.mdf
This is the code I have:
Private Sub FormPrøver3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StudentDataSet.StudentTabell' table. You can move, or remove it, as needed.
Me.StudentTabellTableAdapter.Fill(Me.StudentDataSet.StudentTabell)
Me.StudentTabellTableAdapter.Connection.Close()
Get_Data_Manualy_Fag()
End Sub
Private Sub Get_Data_Manualy_Fag()
Try
'Create variabals for inputcontainer
Dim Studnr As String = Me.StudentnrLabel1.Text
'Create a connection to the DataBase
Dim myConn As SqlConnection
myConn = New SqlConnection("Initial Catalog=OutComes;Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\HovedDatabase.mdf;Integrated Security=True")
'Create a Command object.
Dim myCmd As SqlCommand
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT Fag FROM Fag3Tabell WHERE Fag = #Studnr"
myCmd.Parameters.AddWithValue("#Studnr", Studnr)
'Open the connection.
myConn.Open()
'Process the answer
Dim myreader As SqlDataReader
myreader = myCmd.ExecuteReader
'Traverse the DataSet and Display :
Dim i As Integer = 0
Do While myreader.Read()
Me.ComboBox1.Items.Add(myreader.GetString(i))
i = i + 1
Loop
'Close the connection
myConn.Close()
'Handle errors
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
I set up a similar project with a similar LocalSQL database. I have done nothing in Visual Studio other than creating the database with the same tables.
Here I have no problem connecting to the database via manual connection string.
My question is: why can't I access the local DB with a connection string in the first project?

You haven't posted your other connection string, but it looks like AttachDbFilename may be the problem. The DB will already be attached, this option is only meant to be used in single-user mode (one connection only).
Best bet is to permanently attach the DB to the LocalDB instance if that is what you are using the whole time.

Related

Editing DataGridView data and updating changes directly into MySQL database

I want to save the changes that have made in the datagridview. I have code for adding the existing entries on the datagridview but I don't know how to update the changes made on datagridview
here is the code i am using to for adding the datagridview entries to the database
Public Sub ADD_DGV_CMD(SENDER As String)`
For Each row As DataGridViewRow In ALL_BILL_FRM.DataGridView1.Rows
Dim constring As String = "server=localhost; user=root; password=Masoom1; database=airtech_db; convert zero datetime=true;"
Using con As New MySqlConnection(constring)
Using cmd As New MySqlCommand("Insert into `all_bills` values(null,#Supplier_Name,#Bill_No,#Bill_Type,#Bill_Amount,#Bill_Date);", con)
cmd.Parameters.AddWithValue("#Supplier_Name", row.Cells(1).Value)
cmd.Parameters.AddWithValue("#Bill_No", row.Cells(2).Value)
cmd.Parameters.AddWithValue("#Bill_Type", row.Cells(3).Value)
cmd.Parameters.AddWithValue("#Bill_Amount", row.Cells(4).Value)
cmd.Parameters.AddWithValue("#Bill_Date", row.Cells(5).Value)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
MessageBox.Show("Records inserted.")
End Sub
You keep adding parameters in a loop. If your table contains more than one then all the parameters will be added again. They don't replace the existing ones, they supplement them, hence the "already added" error
Move your parameter adds to outside the loop. Chef get the way you work so that he parameter values are updated in the loop..
I would say "you should do:"
.Parameters("#Supplier_Name").Value = row.Cells(1).Value.ToString
..inside the loop, but what you should actually do is toss this lot out and take a look at using a strongly typed dataset, dapper or entity framework. If you're keen to carry on filling your button click handlers with SQL, dapper lets you do that but you might well get more out of something more high level
The short short version of using datasets is:
add a dataset to your project
open it, right click the surface and choose Add TableAdapter
connect to MySQL, ensure you have installed both the driver and the visual studio tools
enter SELECT * FROM all_bills as the query, finish the wizard
save the dataset
open the forms designer
open the data sources window (view menu, other windows)
drag the grid representing your table out of the data sources window and drop it on the form
run the project
Your grid will load and be able to to read and write the dB, not a single line of code written (by you; visual studio wrote a whole lot behind the scenes - you can see it in various .Designer.vb files)!
Public Class MAIN_FRM
Dim changes_counter As Integer, SEARCH_TYPE As String
Dim con As New MySqlConnection("server=localhost; user=root; password=Masoom1; database=airtech_db;")
Dim da As New MySqlDataAdapter("Select * from all_bills;", con)
Dim ds As New DataSet
Dim cmdBuilder As New MySqlCommandBuilder
Dim LOAD_CMD_CALL As Boolean
Private Sub UPDATE_BTN_Click(sender As Object, e As EventArgs) Handles UPDATE_BTN.Click
Try
cmdBuilder = New MySqlCommandBuilder(da)
ds = ds.GetChanges()
If ds IsNot Nothing Then
da.Update(ds)
MsgBox("Changes Done")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
END SUB

How can I allow multiple users to access/edit a microsoft access database?

Now I know right off the bat that Microsoft access isn't the ideal client for multiple users accessing it but it's the only one I've got right now. I have built a small program as a sort of inventory management system. There are currently three users that will be using it regularly and at the same time. One issue I am running into with this is that sometimes the database will not be accessible and will give an error stating that the file is already in use by "so and so" user. The other issue is that I'm getting a similar error every now and then where it states "The database has been placed in a state by user on machine that prevents it from being opened or locked". I am connecting to the database through an ACE OLEDB connection using the line below
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\Tool & Cutter Grinding\Tool Cutter Database.accdb;Persist Security Info = False"
I have also changed some of the settings in the actual access database such as:
Enable all macros
Add the folder the database is in to the trusted locations list
Confirm that the database is set to open in shared mode by default
I don't know if there is something small I've missed or a setting I need to change but as of yet, the problem is still persisting.
Below is an example of how I am using the database. I am using string based SQL commands but am not too familiar with DataSet/DataTable/etc. items, so I may be doing something incorrectly.
'close connection from any previous session
con.Close()
'clear dataset so as not to append data
ds.Clear()
'Select SQL query that selects ALL records from a table
Dim str As String = "SELECT * FROM " & "[" & table & "]" & ""
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\Tool & Cutter Grinding\Tool Cutter Database.accdb;Persist Security Info = False"
'use try catch statement to open the connection
Try
con.Open()
Catch ex As Exception
MsgBox(Convert.ToString(ex))
End Try
'use try catch statement to add a table (dt) to the dataset (ds) in order to store values
Try
ds.Tables.Add(dt)
Catch ex As Exception
End Try
'create new dataadapter object using the sql string from above and the connection created above
da = New OleDbDataAdapter(str, con)
'create new command builder in order to excecute the SELECT SQL statement using the dataadapter created (da)
'specify prefix and suffix for cb
Dim cb = New OleDbCommandBuilder(da) With {
.QuotePrefix = "[",
.QuoteSuffix = "]"
}
'use try catch statement to fill the datatable (dt) using the dataadapter (da)
Try
da.Fill(dt)
Catch ex As Exception
MsgBox(Convert.ToString(ex))
End Try
'set the datasource of the datagridview to the datatable
dgv.DataSource = dt.DefaultView
'close the connection to the database
con.Close()
Go to your Back-End Access DB file. File > Options > Client Settings. For your Use Case No Locks should be fine, but Edited record setting will work as well if you need it
but its [sic] the only one I've got right now
Actually, it's not.
Have a look at SQL Server Compact. It's free, it's small and it handles multiple users with aplomb.
You can add all the references you need using NuGet.

Opening SQL connection

I have inherited software to maintain. The previous version used a third party Datagridview substitute that isn't compatible with versions of Windows from Vista on. In attempting to put Datagridviews in I have run into a problem with connecting to the database.
I am trying to make a small program to play around with connection and SELECT outside of the original software so I can further understand what I am doing and without going through the full process of using the original software to get to the testing point.
Private Shared Function GetData(ByVal sqlCommand As String) As DataTable
Dim table As New DataTable()
Dim connectionString As String = "Data Source=.\SQLExpress;Integrated Security=true;" _
& "AttachDbFilename=C:blah\blah\blah.mdf;User Instance=true;"
Using con = New SqlConnection(connectionString)
Using command = New SqlCommand(sqlCommand, con)
Using da = New SqlDataAdapter(command)
da.Fill(table)
End Using
End Using
End Using
Return table
End Function
My SQL command is a simple "Select * FROM Setup" and the rest of the program is form loads, imports, and DataGridView formatting. I don't think it affects the SQL part and would be cumbersome to include here.
This results in what appears to be a closed connection.
![Connection Property] http://i.imgur.com/b5V3Qy5.png
This is a screenshot of my SQLExpress which might help diagnose connection problems.
![SQL Properties] http://i.imgur.com/bakBq5D.png
I've blurred out the computer name in grey, but I did notice that there was another computer name in pink. I don't know what it means other than maybe this database was originally created on another computer and has been copied and pasted.
Finally this is the connection string that the original software used:
"Data Source=.\SQLExpress;AttachDbFilename=C:\blah\blah\blah.mdf;Trust_Connection=Yes;"
I have also tried:
"Data Source=.\SQLExpress;AttachDbFilename=C:\blah\blah\blah.mdf;Trusted_Connection=Yes;User Instance=true"
Finally, this is my exception:
"An attempt to attach an auto-named database for file C:\blah\blah\blah.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
I got my alternate connection strings from www.connectionstrings.com.
You are missing the Open() command.
con.Open()
Full code listing. (Also a good idea is to wrap your code in a Try.... End Try block).
Private Shared Function GetData(ByVal sqlCommand As String) As DataTable
Dim table As New DataTable()
Dim connectionString As String = "Data Source=.\SQLExpress;Integrated Security=true;" _
& "AttachDbFilename=C:blah\blah\blah.mdf;User Instance=true;"
Using con = New SqlConnection(connectionString)
conn.Open()
Using command = New SqlCommand(sqlCommand, con)
Using da = New SqlDataAdapter(command)
da.Fill(table)
End Using
End Using
End Using
Return table
End Function

Can't open SQLCompact database

I am working with a SQL compact database and I am able to test the connection when I import it and copy the exact file path to my code, but it still says that it hasn't been opened. What am I doing wrong? Are there any shortcuts available if I have already added the database to my project?
Thanks!
Imports System.Data.SqlServerCe
Module Module1
Sub Main()
Dim constring As String = "Data Source=C:\Users\test\Desktop\MyDatabase1.sdf"
Dim conn As New SqlCeConnection(constring)
Dim cmd As New SqlCeCommand("SELECT * FROM ACCOUNT")
conn.Open()
Dim reader As SqlCeDataReader = cmd.ExecuteReader()
While reader.Read()
Console.WriteLine(reader)
End While
End Sub
End Module
You need to assign the connection to the command:
Immediately after this line:
conn.Open()
add:
cmd.Connection = conn
Alternatively, you can add the connection to the command's constructor:
Dim cmd As New SqlCeCommand("SELECT * FROM ACCOUNT", conn)
One of the main advantage of using SQL server CE (3.5) is to have Linq to SQL. You should make use of strongly typed database, and of DataContext. if you do so, then creating a new DataContext is ONE line of code, not 8. And if your DataBase file does not exist, the CreateDataTable method of your DataContext will create them for you. Dig a little into this, because using SQL Server CE like an old fashion OLEDB data provider is... well... not optimal :-)

ADO.Net Synchronization & SQL CE

I've succesfully synchronized both source and local db using the local database cache item in VS 2008.
However, I need to access the SQL CE db directly from within another dll/process, and without using a dataset. The reason being that my business object code does not use datasets.
The final code wouldlook something like this:
Dim conn As New SqlServerCe.SqlCeConnection("Data Source=C:\Development\UserDirectory\UserDirectory.DBSyncher\ProfDir.sdf;Persist Security Info=False;")
Dim cmd As New SqlServerCe.SqlCeCommand("Select EmailAddress from Employees Where ID=23", conn)
Dim returnString As String = ""
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
returnString = cmd.ExecuteScalar
conn.Close()
cmd = Nothing
I notice something very strange using a dataset the synchronized changes are shown but accessing the CE database file directly returns old data - no synched data whatsoever.
What am I missing? Any help would be greatly appreciated.
Figured it out!
Forgot that CE is in process, thus it copies the database file(.sdf) to the Debug folder. You have to to reference that database not the one in your project. DOH!