表保存在master数据库中 [英] Table save in master database

查看:148
本文介绍了表保存在master数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的数据库名称是TestScript但是当我创建表格时

表保存在Master数据库中

我想保存在TestScript数据库中< br $> b $ b

我尝试过:



进口System.Configuration 
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理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

使用SqlCon作为的SqlConnection =新的SqlConnection(SQLConnectionString)
尺寸STR作为字符串
STR = CREATE DATABASE SQLSqript ON PRIMARY(NAME = SQLSqript_Data,FILENAME =C: \ COMPUTECH \ SQLSqript.mdf',& _
SIZE = 2MB,MAXSIZE = 10MB,FILEGROWTH = 10%)登录(NAME = SQLSqript_Log,FILENAME ='C:\ COMPUTECH \SQLSqript.Log',& _
SIZE = 1MB,MAXSIZE = 5MB,FILEGROWTH = 10%)
尺寸CMD作为的SqlCommand =新的SqlCommand(STR,SqlCon)
尝试
cmd.Connection.Open ()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Catch
MsgBox(已安装的数据库,MsgBoxStyle.Critical)
结束尝试

尝试
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(已安装的数据库,MsgBoxStyle.Critical)
结束尝试
结束使用
End Sub
结束类

解决方案

一些事情:

1)为了解决你的问题,最简单的方法是使用两个独立的SqlConnection对象 - 一个像现有的一样连接到Master,另一个初始目录设置为 SQLSqript 。仅使用现有的数据库创建数据库,然后使用新数据库创建数据库。

2)不要使用 sa 用户:为具有不同访问级别的数据库用户创建新帐户,并完全禁用 sa 帐户。您应该只连接到具有足够权限来完成工作的帐户,而不是完全访问。并且 sa 帐户是黑客开始寻找的帐户,因为它只是存在表明系统不安全!


 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


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

解决方案

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!


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"


这篇关于表保存在master数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆