-
Mar 2nd, 2021, 09:36 AM
#1
Thread Starter
Addicted Member
Create a Database Programmatically and
I am learning VB2019 Student edition and it is the very first time that I interact with the VB2019 code. The project to do is to Create a Database and a table programmatically separately. One program for creating a database and create a table. So, I just started with the database, and I got this code from a tutorial over the internet and I just compiled it but, I got this error BC30002 Type 'sqlconnection' is not defined. I was looking for some references on the internet and I did not find this error. Can somebody with visual basic knowledge help me, please?
Public Class Form1
Private Sub btnCreateDatabase_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnCreateDatabase.Click
Dim str As String
Dim myConn As SqlConnection = New SqlConnection("Server=(local)\netsdk;" &
"uid=sa;pwd=;database=master")
str = "CREATE DATABASE MyDatabase ON PRIMARY " &
"(NAME = MyDatabase_Data, " &
" FILENAME = 'D:\MyFolder\MyDatabaseData.mdf', " &
" SIZE = 2MB, " &
" MAXSIZE = 10MB, " &
" FILEGROWTH = 10%)" &
" LOG ON " &
"(NAME = MyDatabase_Log, " &
" FILENAME = 'D:\MyFolder\MyDatabaseLog.ldf', " &
" SIZE = 1MB, " &
" MAXSIZE = 5MB, " &
" FILEGROWTH = 10%)"
Dim myCommand As SqlCommand = New SqlCommand(str, myConn)
Try
myConn.Open()
myCommand.ExecuteNonQuery()
MessageBox.Show("Database is created successfully",
"MyProgram", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
If (myConn.State = ConnectionState.Open) Then
myConn.Close()
End If
End Try
-
Mar 2nd, 2021, 12:24 PM
#2
Re: Create a Database Programmatically and
Put this on top of your code:
Imports System.Data.SqlClient
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Mar 2nd, 2021, 09:07 PM
#3
Thread Starter
Addicted Member
Re: Create a Database Programmatically and
Originally Posted by sapator
Put this on top of your code:
Imports System.Data.SqlClient
Yes, I did I put it right on top and now I got all these messages, see this, please?
-
Mar 2nd, 2021, 09:48 PM
#4
Re: Create a Database Programmatically and
Please check your connection string:
https://www.connectionstrings.com/sql-server/
At least this: Server=(local)\netsdk;
I believe should be this:Server=(local); Database=netsdk;
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Mar 2nd, 2021, 09:53 PM
#5
Re: Create a Database Programmatically and
For future reference, please don't post only pictures of error messages. You can provide a picture as well, if it adds some value, but you should ALWAYS provide the error message as text. If we want to search the web for that error message, we can't copy and paste from a picture.
Anyway, that means that there's an issue with your connection string. The error message is basically telling you what the issue is, where it happened and what to do about it. Either you have specified a database or instance that doesn't exist or is inaccessible or you have provided credentials that don't provide the required permissions.
By the way, that's not "all these messages". It's one message. The text provides the stack trace of the exception, which is the list of methods that were executing at the time. If you look right at the bottom you can see your method, i.e. btnCreateDatabase_Click. The next frame is SqlConnection.Open, which your method calls. The remaining frames are all the methods that get called internally from Open until the exception is thrown in the SqlInternalConnectionTds constructor.
-
Mar 2nd, 2021, 09:55 PM
#6
Re: Create a Database Programmatically and
Originally Posted by sapator
The database is master, which is also the default if a database isn't specified. You generally wouldn't connect to an existing database to create a new database.
-
Mar 2nd, 2021, 10:35 PM
#7
Re: Create a Database Programmatically and
Aha. i missed the database string that it was at the end.
So first try to check you configuration on TCP:
Go to Start->Programs->Microsoft SQL Server 2005/2008/2012 ->Configuration Tools and select the SQL Server Configuration Manager:
Under the SQL Server Network Configuration select Protocols for <your server name>:
Make sure that TCP/IP protocol is enabled (lot of info from here: https://knowledgebase.apexsql.com/co...apexsql-tools/)
and then read this: https://support.microsoft.com/en-us/...2-22e1694bb889
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|