VB.NET没有在sql server 2008 express中创建数据库 [英] VB.NET not creating database in sql server 2008 express
问题描述
我正在开发一个vb.net winform项目,以详细了解访问者的业务。我正在使用sql server 2008 express数据库来存储详细信息。
当应用程序启动时,它会检查数据库,如果它在那里继续,但如果它不是它在sql server中创建数据库2008快递。
附加的代码应该创建数据库,如果它不存在(好吧,无论如何应该)。该程序将通过应该创建数据库的代码没有问题,但会在sqlCon.Open的表单加载中抛出错误,错误是:'无法打开数据库访问者登录请求。登录失败。如果有人能告诉我哪里出错了,我将不胜感激?
私人 Sub allVisitors_Load(发件人作为 Object ,e As EventArgs)句柄 MyBase .Load
Me .Text = 所有访客
如果 不 checkDatabaseExists()然后
Dim Result1 = MessageBox。显示( 数据库不存在,按确定继续并创建数据库。按取消关闭。, 创建数据库,MessageBoxButtons.OKCancel)
If Result1 = Windows.Forms.DialogResult.OK 然后
createVisitorsDatabase() ' 调用sub来创建数据库
createTablesForVisitors()' 调用sub以在数据库中创建表
ElseIf Result1 = Windows.Forms.DialogResult.Cancel 然后
我 .Close()
退出 Sub
结束 如果
MessageBox.Show( 数据库已创建。按确定继续。, 数据库创建,MessageBoxButtons.OK)
Else
Dim Result2 = MessageBox.Show( 存在Datebase,按确定继续。按取消关闭。, Dateabase exists,MessageBoxButtons.OKCancel)
如果 Result2 = Windows。 Forms.DialogResult.OK 然后
ElseIf Result2 = Windows.Forms.DialogResult .Cancel 然后
我 .Close()
退出 Sub
结束 如果
结束 如果
connectionString = 数据源= .\SQLExpress; Initial Catalog = Visitors; Integrated Security = True; MultipleActiveResultSets = True
sql = < span class =code-string> SELECT idNumber,firstName,lastName,company FROM visitorDetails
sqlCon = 新 SqlConnection (connectionString)
sqlCon.Open()' 此处抛出错误。
' 错误是:无法打开登录请求的数据库访客。登录失败。
sqlCmd = 新 SqlCommand(sql,sqlCon)
da = < span class =code-keyword>新 SqlDataAdapter(sql,sqlCon)
dt = loadDtVisitorDetails()
fillDgvVisitorDetails(dt)
sqlCmd .Dispose()
sqlCon.Close()
结束 Sub
公共 Sub createVisitorsDatabase()
connectionString = Data Source = .\SQLExpress; Integrated Security = True; MultipleActiveResultSets = True
sql = CREATE DATABASE Visitors
sqlCon = 新 SqlConnection(connectionString)
sqlCo n.Open()
sqlCmd = 新 SqlCommand(sql,sqlCon)
da = 新 SqlDataAdapter(sql,sqlCon)
sqlCmd.Dispose()
sqlCon.Close()
结束 Sub
公开 Sub createTablesForVisitors()
connectionString = 数据Source = .\SQLExpress; Integrated Security = True; MultipleActiveResultSets = True
sql = 使用访问者& _
CREATE TABLE visitorDetails& _
(idNumber int NOT NULL,firstName varchar(25)NOT NULL,lastName varchar(40)NOT NULL,公司varchar(150)NOT NULL,& _
contactNumber varchar( 50)NOT NULL,countryCode varchar(1000)NOT NULL,photoId varchar(1000)NULL,email varchar(150)NULL,PRIMARY KEY(idNumber))& _
CREATE TABLE reasonForVisit& _
(idNumber int,dateOnSite varchar(20),reasonForVisit varchar(150))
sqlCon.Open()
sqlCmd = 新 SqlCommand(sql,sqlCon)
da = 新 SqlDataAdapter(sql,sqlCon)
sqlCmd.Dispose()
sqlCon.Close()
结束 Sub
< blockquote>你似乎永远不会执行命令。
而不是:
da =新的SqlDataAdapter(sql,sqlCon)
投入
sqlCmd.ExecuteNonQuery()
这同样适用于你 createTablesForVisitors
方法。
替换
da =新的SqlDataAdapter(sql ,sqlCon)
with
SQLCmd.Execu teNonQuery()
SQLDataAdapter表示一组数据命令和数据库连接,用于填充DataSet和更新一个SQL Server数据库。它不应该用于执行DDL语句。
I am developing a vb.net winform project to take in details of visitors into a business. I am using a sql server 2008 express database to store the details.
When the application starts, it checks for a database, if it's there it continues on but if it's not it creates the database in sql server 2008 express.
The code attached is supposed to create the database if it doesn't exist (well, it's supposed to anyway). The program will go through the code that's supposed to create the database no problem but will throw an error in the form load at sqlCon.Open, the error is: 'Cannot open database "Visitors" requested by the login. The login failed.' It would be greatly appreciated if someone could tell me where I am going wrong?
Private Sub allVisitors_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "All Visitors"
If Not checkDatabaseExists() Then
Dim Result1 = MessageBox.Show("Database does not exist, press OK to continue and create database. Press Cancel to close.", "Create database", MessageBoxButtons.OKCancel)
If Result1 = Windows.Forms.DialogResult.OK Then
createVisitorsDatabase() 'Calls sub to create database
createTablesForVisitors() 'Calls sub to create tables in database
ElseIf Result1 = Windows.Forms.DialogResult.Cancel Then
Me.Close()
Exit Sub
End If
MessageBox.Show("Database has been created. Press OK to continue.", "Database created", MessageBoxButtons.OK)
Else
Dim Result2 = MessageBox.Show("Datebase exists, press OK to continue. Press Cancel to close.", "Dateabase exists", MessageBoxButtons.OKCancel)
If Result2 = Windows.Forms.DialogResult.OK Then
ElseIf Result2 = Windows.Forms.DialogResult.Cancel Then
Me.Close()
Exit Sub
End If
End If
connectionString = "Data Source=.\SQLExpress;Initial Catalog=Visitors;Integrated Security=True;MultipleActiveResultSets=True"
sql = "SELECT idNumber, firstName, lastName, company FROM visitorDetails"
sqlCon = New SqlConnection(connectionString)
sqlCon.Open() 'Error thrown here.
'Error is : Cannot open database "Visitors" requested by the login. The login failed.
sqlCmd = New SqlCommand(sql, sqlCon)
da = New SqlDataAdapter(sql, sqlCon)
dt = loadDtVisitorDetails()
fillDgvVisitorDetails(dt)
sqlCmd.Dispose()
sqlCon.Close()
End Sub
Public Sub createVisitorsDatabase()
connectionString = "Data Source=.\SQLExpress;Integrated Security=True;MultipleActiveResultSets=True"
sql = "CREATE DATABASE Visitors"
sqlCon = New SqlConnection(connectionString)
sqlCon.Open()
sqlCmd = New SqlCommand(sql, sqlCon)
da = New SqlDataAdapter(sql, sqlCon)
sqlCmd.Dispose()
sqlCon.Close()
End Sub
Public Sub createTablesForVisitors()
connectionString = "Data Source=.\SQLExpress;Integrated Security=True;MultipleActiveResultSets=True"
sql = " USE Visitors " & _
"CREATE TABLE visitorDetails " & _
"(idNumber int NOT NULL, firstName varchar(25) NOT NULL, lastName varchar(40) NOT NULL, company varchar(150) NOT NULL, " & _
"contactNumber varchar(50) NOT NULL, countryCode varchar(1000) NOT NULL, photoId varchar(1000) NULL, email varchar(150) NULL, PRIMARY KEY (idNumber)) " & _
"CREATE TABLE reasonForVisit " & _
"(idNumber int, dateOnSite varchar(20), reasonForVisit varchar(150))"
sqlCon.Open()
sqlCmd = New SqlCommand(sql, sqlCon)
da = New SqlDataAdapter(sql, sqlCon)
sqlCmd.Dispose()
sqlCon.Close()
End Sub
You never appear to execute the command.
instead of:
da = New SqlDataAdapter(sql, sqlCon)
put in
sqlCmd.ExecuteNonQuery()
The same applies to yourcreateTablesForVisitors
method.
Replace
da = New SqlDataAdapter(sql, sqlCon)
with
SQLCmd.ExecuteNonQuery()
SQLDataAdapter represents a set of data commands and a database connection that are used to fill a DataSet and update a SQL Server database. It should not be used to execute DDL statements.
这篇关于VB.NET没有在sql server 2008 express中创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!