KarlTheHopeless
asked on
adding rows to new database with visual basic
Hello People
I have just created a new database called database1.sdf with just two records.
I did this using the IDE in Visual Studio.
But I now want to add a few hundred records that reside in a file to this new database, and I do not even know how to begin to do this.
I am competent in Visual Basic, but I am clueless about databases.
How should I begin? Where should I look?
Thank you
Karl
Capture.PNG
I have just created a new database called database1.sdf with just two records.
I did this using the IDE in Visual Studio.
But I now want to add a few hundred records that reside in a file to this new database, and I do not even know how to begin to do this.
I am competent in Visual Basic, but I am clueless about databases.
How should I begin? Where should I look?
Thank you
Karl
Capture.PNG
ASKER
I don't think that even gets me to first base.
Don't I need to connect to the database, or something? - before I can do anything.
Don't I need to connect to the database, or something? - before I can do anything.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That doesn't seem appropriate really.
I need to read a very big file of records and then chop each RECORD into the various FIELDS of the new database.
My problem is that I do not know how to OPEN this database and Read/Write to it using Visual Basic.
I need to read a very big file of records and then chop each RECORD into the various FIELDS of the new database.
My problem is that I do not know how to OPEN this database and Read/Write to it using Visual Basic.
ASKER
Hi Kalpesh
I tried this, but it failed to connect, ...
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = "Data Source = C:\Users\karl\Documents\Vi sual Studio 2010\Projects\Pupils\Pupil s\Database 1.sdf"
con.Open()
cmd.Connection = con
I tried this, but it failed to connect, ...
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = "Data Source = C:\Users\karl\Documents\Vi
con.Open()
cmd.Connection = con
ASKER
I got this error message ...
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
But I do not know what to do about it.
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
But I do not know what to do about it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ha. I'm almost there, having found the code below elsewhere.
My *only* problem now seems to be with the INSERT bit in the following code.
I've obviously misunderstood how it works,
.....
Dim con As New System.Data.SqlServerCe.Sq lCeConnect ion("Data Source=C:\Users\karl\Docum ents\Visua l Studio 2010\Projects\Pupils\Pupil s\Database 1.sdf")
con.Open()
Dim cmd As System.Data.SqlServerCe.Sq lCeCommand
'Create an instance of SqlCeDataReader
Dim dtr As System.Data.SqlServerCe.Sq lCeDataRea der
'INSERT
cmd = New System.Data.SqlServerCe.Sq lCeCommand ("INSERT INTO BasicTable (ParentID,FirstName,Second Name) VALUES (3,john,smith)", con)
cmd.ExecuteNonQuery()
'Retrieve data from BasicTable
cmd = New System.Data.SqlServerCe.Sq lCeCommand ("SELECT FirstName FROM BasicTable", con)
dtr = cmd.ExecuteReader()
'Read and display the data
While dtr.Read()
MsgBox(dtr("FirstName"))
End While
'Close the DataReader
dtr.Close()
'Close the database connection
con.Close()
My *only* problem now seems to be with the INSERT bit in the following code.
I've obviously misunderstood how it works,
.....
Dim con As New System.Data.SqlServerCe.Sq
con.Open()
Dim cmd As System.Data.SqlServerCe.Sq
'Create an instance of SqlCeDataReader
Dim dtr As System.Data.SqlServerCe.Sq
'INSERT
cmd = New System.Data.SqlServerCe.Sq
cmd.ExecuteNonQuery()
'Retrieve data from BasicTable
cmd = New System.Data.SqlServerCe.Sq
dtr = cmd.ExecuteReader()
'Read and display the data
While dtr.Read()
MsgBox(dtr("FirstName"))
End While
'Close the DataReader
dtr.Close()
'Close the database connection
con.Close()
ASKER
Got it.
The apostrophes were missing.
The apostrophes were missing.
ASKER
Thank you.
kalpesh2804
Thank you for your response to this thread. I'm not having the same issue, but did find the link to datasets, collections, and such very helpful. I'm an old Visual Studio 6 developer C++, VB. Didn't do any .net stuff, just ADO, and ODBC. I found the .net / collections & dataset article very helpful at getting my head around DATASETS & COLLECTIONS. I can really see the value.
Thanx again
Mark
Thank you for your response to this thread. I'm not having the same issue, but did find the link to datasets, collections, and such very helpful. I'm an old Visual Studio 6 developer C++, VB. Didn't do any .net stuff, just ADO, and ODBC. I found the .net / collections & dataset article very helpful at getting my head around DATASETS & COLLECTIONS. I can really see the value.
Thanx again
Mark
Sample
insert into BasicTable(Title,FirstName