VB.NET 不在 sql server 2008 express 中创建数据库
Posted
技术标签:
【中文标题】VB.NET 不在 sql server 2008 express 中创建数据库【英文标题】:VB.NET not creating database in sql server 2008 express 【发布时间】:2014-08-13 13:47:21 【问题描述】:我正在开发一个 vb.net winform 项目,以将访客的详细信息纳入企业。我正在使用 sql server 2008 express 数据库来存储详细信息。 当应用程序启动时,它会检查数据库,如果存在则继续,如果不存在则在 sql server 2008 express 中创建数据库。 如果数据库不存在,则附加的代码应该创建数据库(好吧,无论如何都应该这样做)。该程序将通过应该创建数据库没有问题的代码,但会在 sqlCon.Open 的表单加载中引发错误,错误是:'无法打开登录请求的数据库“访问者”。登录失败。如果有人能告诉我哪里出错了,将不胜感激?
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
【问题讨论】:
您好 - 检查您登录的默认数据库,并确保它没有设置(或空白)为您无权访问或不存在的数据库。如果不存在,则为空白,将其更改为 master 并重新测试。 【参考方案1】:换行
da = New SqlDataAdapter(sql, sqlCon)
与
sqlCmd.ExecuteNonQuery()
执行您的 CREATE DATABASE。
【讨论】:
以上是关于VB.NET 不在 sql server 2008 express 中创建数据库的主要内容,如果未能解决你的问题,请参考以下文章
链接到 SQL Server 的 Active Directory
从 vb.net 中的 datetime sql server 类型操作日期和时间
如何使用 vb.net 和 SQL Server 裁剪和调整图像大小
VB.NET操作SQL SERVER 2008数据库,想把文本框中输入的字符串转化为数字进行数据库的更新,却一直失败