在Vb.Net中尝试为文件附加自动命名数据库失败 [英] An attempt to attach an auto-named database for file failed in Vb.Net

查看:70
本文介绍了在Vb.Net中尝试为文件附加自动命名数据库失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试第一次连接数据库,但出现此错误:

I am Trying to connect database for first time , and I am getting this error :


尝试附加自动命名的文件VBTestDB.mdf的数据库失败。存在具有相同名称的数据库,或者无法打开指定的文件,或者该文件位于UNC共享上。

An attempt to attach an auto-named database for file VBTestDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

并出现错误


myconnect.Open()

myconnect.Open()

这是我的代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myconnect As New SqlClient.SqlConnection
    myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=VBTestDB.mdf;Integrated Security=True;User Instance=True;"


    Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
    mycommand.Connection = myconnect
    mycommand.CommandText = "INSERT INTO Card (CardNo,Name) VALUES (@cardno,@name)"
    myconnect.Open()

    Try
        mycommand.Parameters.Add("@cardno", SqlDbType.Int).Value = TextBox1.Text
        mycommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = TextBox2.Text

        mycommand.ExecuteNonQuery()
        MsgBox("Success")
    Catch ex As System.Data.SqlClient.SqlException
        MsgBox(ex.Message)
    End Try
    myconnect.Close()
End Sub


推荐答案

我能够解决我的问题。
我只是将连接字符串更改为:

I am able to solve my problem. I just change my connection string to :


myconnect.ConnectionString =数据源= .\SQLEXPRESS;初始目录= VBTestDB; Integrated Security = True;用户ID = sa; Password = welcome1

myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=VBTestDB;Integrated Security=True;User Id=sa;Password=welcome1"

,它工作正常。

谢谢

这篇关于在Vb.Net中尝试为文件附加自动命名数据库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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