[英]Create new Database in SQL Server 2008 R2
I use the following code to create a new database in SQL Server 2008 R2, program language is VB.NET: 我使用以下代码在SQL Server 2008 R2中创建一个新数据库,程序语言为VB.NET:
Dim WEBconnString As String = "Data Source='NewName'; LCID=1032; Case Sensitive = FALSE; SSCE:Database Password=NewDBpass; Encrypt = TRUE;"
Dim engine As New SqlCeEngine(WEBconnString)
engine.CreateDatabase()
Additionally: 另外:
I open a web server connection with the following instruction: 我按照以下说明打开Web服务器连接:
ServerConn.ConnectionString = "Provider=SQLOLEDB; Data Source=" & WebServerName & _
"; Database=master" & _
"; Uid=" & TempUserName & _
"; Pwd=" & TempUserPass
ServerConn.Open()
And I also use a data base connection with the following instruction: 我还使用数据库连接,并遵循以下指令:
SQLConn.ConnectionString = "server=" & WebServer & "; Uid=" & AdminName & "; Pwd=" & AdminPassword & "; database=master"
SQLConn.Open()
In order to be able to use my instruction. 为了能够使用我的指令。 I have already create a temporary database in my ISP SQL Server, and I'm using for login name and password the credentials from this database. 我已经在ISP SQL Server中创建了一个临时数据库,并且正在使用该数据库的凭据作为登录名和密码。
For the first time use; 第一次使用; it works fine, means that somewhere creates a database 它工作正常,意味着在某处创建数据库
Now I'm trying to see this database and I can't found anything 现在,我正在尝试查看该数据库,但找不到任何内容
I'm running the same code again because it seams to me that the database not created, and at the engine instruction gives me an error "The database exist" 我再次运行相同的代码,因为它向我暗示未创建数据库,并且在引擎指令处给我一个错误“数据库存在”
And the question is: Where it is opened the new database? 问题是:在哪里打开新数据库?
Please give me any solution I may need in VB.NET 请给我我在VB.NET中可能需要的任何解决方案
1) It seems you are mixing SqlServerCompact (Local datatase which can be used in ASP.NET since version 4.0) and SqlServer . 1)似乎您正在混合使用SqlServerCompact (从4.0版开始可以在ASP.NET中使用的本地数据仓库)和SqlServer 。 SqlCeEngine
is part of System.Data.SqlServerCe
namespace. SqlCeEngine
是System.Data.SqlServerCe
命名空间的一部分。 So you create a SqCompact file and the engine.CreateDatabase()
method raises an exception the second time. 因此,您创建了一个SqCompact文件,并且engine.CreateDatabase()
方法第二次引发异常。 The connection string seems correct (for a SqlServerCompact file). 连接字符串似乎正确(对于SqlServerCompact文件)。 If you don't specify the full path in your connection string (just set the database name like here), the database will be created where the app.exe
is executed (= |DirectoryPath|
). 如果未在连接字符串中指定完整路径(只需在此处设置数据库名称),则将在执行app.exe
位置创建数据库(= |DirectoryPath|
)。 You will have to look for a file with .sdf
extension. 您将需要查找扩展名为.sdf
的文件。
2) I don't know what type is ServerConn
but since I see "Provider=SQLOLEDB;"
2)我不知道ServerConn
是什么类型,但是因为看到"Provider=SQLOLEDB;"
in your connection string, I guess your are using OLEDB
class. 在您的连接字符串中,我想您正在使用OLEDB
类。 You should use instead the managed SqlServer class ( System.Data.SqlServer
namespace). 您应该改用托管的SqlServer类( System.Data.SqlServer
命名空间)。 So you should use SqlConnection
, SqlCommand
, ..., objects. 因此,您应该使用SqlConnection
, SqlCommand
,...对象。 If you already use them, then check your connectionstring since the provider is wrong. 如果您已经使用它们,则请检查您的连接字符串,因为提供者有误。 At any rate, you can't access to the file created first in 1) in both case. 无论如何,在两种情况下,您都无法访问在1)中首先创建的文件。
3) If your goal is to create a SqlServer Datatase, unfortunatly, where is no SqlServerEngine
class like in SqlServerCe
. 3)不幸的是,如果您的目标是创建SqlServer Datatase,那么在SqlServerCe
中就没有SqlServerEngine
类。
To create a database, some possible ways: 要创建数据库,可以采用以下几种方法:
System.Data.SqlServer.Smo
class (.Net) 使用System.Data.SqlServer.Smo
类(.Net) It looks like you are opening the Server connection the the master database. 似乎您正在打开与master数据库的服务器连接。 The master database is where the sql server engine keeps all the important information and meta data about all databases on that server. 主数据库是sql服务器引擎保存有关该服务器上所有数据库的所有重要信息和元数据的地方。
I need to replace database=master
to database=WHATEVER DATABASE NAME YOU CREATED
. 我需要将database=master
替换为database=WHATEVER DATABASE NAME YOU CREATED
。 All else fails go to the sql server and run this 其他所有失败均转到sql服务器并运行
use master;
select * from sysdatabases
That will give you the name of every database that master is 'monitoring'. 这将为您提供master正在“监视”的每个数据库的名称。 Find the one you tried to create and replace the database=master
with database=the one you found
找到您尝试创建的database=master
然后将database=master
替换为database=master
database=the one you found
Here is a ready to use sub for creating a procudere: 这是一个易于使用的子程序,用于创建过程:
Public Shared Sub CreateSP(ByVal usp As String, ByVal sender As Object, ByVal e As System.EventArgs)
Dim RetValue As Boolean = False
Try
DBConnection("SQLConn", "SQLUSER", "master", False, sender, e)
Select Case SQLConn.State
Case ConnectionState.Open
MASQLComm = New SqlCommand("CREATE PROCEDURE [dbo].[" & usp & "]" & _
NewLine & " ( " & _
NewLine & "@DBName varchar(50) ) AS " & NewLine & _
"BEGIN_TRY: " & NewLine & " SET QUOTED_IDENTIFIER ON;" & NewLine & " SET NOCOUNT ON; " & _
NewLine & "DECLARE " & _
NewLine & "@ErrMsg nvarchar(4000), " & _
NewLine & "@CreateSdb nvarchar(4000)" & _
NewLine & "SET @CreateSdb =('CREATE DATABASE ' + @DBName )" & _
NewLine & "BEGIN_CATCH:" & NewLine & "SELECT ERROR_MESSAGE() as ErrorMessage;" & _
NewLine & "SELECT @ErrMsg = ERROR_MESSAGE()" & _
NewLine & "EXEC sp_executesql @CreateSdb" & _
NewLine & "RAISERROR (@ErrMsg,2,1)" & _
NewLine & "Return 0" & NewLine & "END_CATCH:" & NewLine & "Return 1" & NewLine & "END_TRY: ", SQLConn)
If MASQLComm.Connection.State = ConnectionState.Open Then
MASQLComm.ExecuteNonQuery()
Else
MASQLComm.Connection.Open()
MASQLComm.ExecuteNonQuery()
End If
End Select
Success = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Class 'DB_Access' CreateSP ", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Success = False
End Try
MASQLComm.Connection.Close()
End Sub
And here is a code for use to run the above procedure: 这是用于运行上述过程的代码:
Public Shared Sub RunSP(ByVal DrivePath As String, ByVal DBname As String, ByVal sender As Object, ByVal e As System.EventArgs)
Try
DBConnection("SQLConn", "SQLUSER", "master", False, sender, e)
MASQLComm = New SqlCommand("EXEC dbo." & procName & " '" & DBname & " '", SQLConn)
MASQLComm.ExecuteNonQuery()
MASQLComm.ExecuteNonQuery()
MASQLComm = New SqlCommand("ALTER DATABASE " & DBname & " SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE", SQLConn)
MASQLComm.ExecuteNonQuery()
Success = True
Catch qx As SqlException
MessageBox.Show(qx.Message, "Class 'NainClass' Run Stored Procdure", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Success = False
End Try
MASQLComm.Connection.Close()
End Sub
And here is a code for SQLconn connectionstring: 这是SQLconn connectionstring的代码:
If Not CBool(SQLConn.State) Then
SQLConn.ConnectionString = "server=" & ServNameKeyValue & "; Uid=" & cUserName & "; Pwd=" & cUserPass & "; database=" & DBname
SQLConn.Open()
Else
SQLConn.Close()
SQLConn.ConnectionString = "server=" & ServNameKeyValue & "; Uid=" & cUserName & "; Pwd=" & cUserPass & "; database=" & DBname
SQLConn.Open()
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.