Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am creating database name is "TestScript" but when i create table
table save in "Master" Database
I want to save in "TestScript" database

What I have tried:

Imports System.Configuration
Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim SQLConnectionString As String = "Data Source=COMPUTECH-PC\SQLEXPRESS;Initial Catalog=Master;Integrated Security=true;User ID=sa;Password=", providerName = "System.Data.SqlClient"
       
        Using SqlCon As SqlConnection = New SqlConnection(SQLConnectionString)
              Dim str As String
            str = "CREATE DATABASE SQLSqript ON PRIMARY (NAME = SQLSqript_Data, FILENAME = 'C:\COMPUTECH\SQLSqript.mdf', " & _
            " SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) LOG ON(NAME = SQLSqript_Log, FILENAME = 'C:\COMPUTECH\SQLSqript.Log'," & _
            " SIZE = 1MB,MAXSIZE = 5MB, FILEGROWTH = 10%) "
            Dim cmd As SqlCommand = New SqlCommand(str, SqlCon)
            Try
                cmd.Connection.Open()
                cmd.ExecuteNonQuery()
                cmd.Connection.Close()
            Catch
                MsgBox(" Already installed database", MsgBoxStyle.Critical)
            End Try

            Try
                Dim sql As String
                sql = "CREATE TABLE [dbo].[Script]([Name] [nvarchar](50) NULL,[Address] [nvarchar](50) NULL,[Phone] [nvarchar](50) NULL) ON [PRIMARY]"

                cmd = New SqlCommand(sql, SqlCon) 
                cmd.Connection.Open()
                cmd.ExecuteNonQuery()
                cmd.Connection.Close()
            Catch
                MsgBox(" Already installed database", MsgBoxStyle.Critical)
            End Try
        End Using
    End Sub
End Class
Posted
Updated 20-Sep-18 19:51pm

A couple of things:
1) To fix your problem, the easiest way is to use two separate SqlConnection objects - one that connects to Master like your existing one does, and one with the initial catalog set to SQLSqript. Use the existing one only to create the DB, then the new one to create the tables.
2) Don't use the sa user: create new accounts for your DB users with varying levels of access, and disable the sa account completely. You should only ever connect to an account which has just enough permission to do the job, never "full access". And the sa account is the one that hackers will start off looking for, as its mere presence indicates an insecure system!
 
Share this answer
 
Comments
Computechsoft 21-Sep-18 1:50am    
First Query is wrong because i am using Catalog="Master"
Second query is correct Catalog="TestScript"
Therefor table were save in Master table
Thanks OriginalGriff for Solution
OriginalGriff 21-Sep-18 2:08am    
You're welcome!
Dim SQLConnectionStringTeble As String = "Data Source=SOFTLINKS-PC\SQLEXPRESS;Initial Catalog=Master;Integrated Security=true;User ID=sa;Password=", providerName = "System.Data.SqlClient"


**************************************************************************
Dim SQLConnectionStringTeble As String = "Data Source=SOFTLINKS-PC\SQLEXPRESS;Initial Catalog=TestScript;Integrated Security=true;User ID=sa;Password=", providerName = "System.Data.SqlClient"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900