Visual studio 2010 如何在VisualStudio2005中保存到SQLServerExpress数据库

Visual studio 2010 如何在VisualStudio2005中保存到SQLServerExpress数据库,visual-studio-2010,sql-server-express,sqldatasource,Visual Studio 2010,Sql Server Express,Sqldatasource,我是vb.net的初学者,想知道如何在Visual Studio 2005中将窗口窗体连接到SQL Server Express数据库,以便我们以后可以对记录执行添加、搜索、更新和删除 这是我的密码: Dim mycommand As SqlCommand Dim dr As SqlDataReader Dim dr1 As SqlDataReader Dim ra As Integer Private Sub cmdsave_Click(ByVal sender As System.Obje

我是vb.net的初学者,想知道如何在Visual Studio 2005中将窗口窗体连接到SQL Server Express数据库,以便我们以后可以对记录执行添加、搜索、更新和删除

这是我的密码:

Dim mycommand As SqlCommand
Dim dr As SqlDataReader
Dim dr1 As SqlDataReader
Dim ra As Integer

Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)       Handles cmdsave.Click

    Using myconnection As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database\app.mdf;Integrated Security=True;User Instance=True")
        myconnection.Open()
        mycommand = New SqlCommand("insert into staff([FirstName],[LastName],[Address],[DOB], " & _
                                   "[TelephoneNum], [DateJoinIn], [HighestQualifi], [AppointedAs], [Salary]) " & _
                                   "VALUES (@first, @last, @address, @dob, @tel, @dateJ, @highQ, @appointed, @sal)", myconnection)

        mycommand.Parameters.AddWithValue("@first", txtfname.Text)
        mycommand.Parameters.AddWithValue("@last", txtlname.Text)
        mycommand.Parameters.AddWithValue("@address", txtaddress.Text)
        mycommand.Parameters.AddWithValue("@dob", txtdob.Text)
        mycommand.Parameters.AddWithValue("@tel", txttelephone.Text)
        mycommand.Parameters.AddWithValue("@dateJ", txtdjoinin.Text)
        mycommand.Parameters.AddWithValue("@highQ", txthqualifi.Text)
        mycommand.Parameters.AddWithValue("@appointed", txtappoint.text)
        mycommand.Parameters.AddWithValue("@sal", txtsalary.Text)
        mycommand.ExecuteNonQuery()

        myconnection.Close()
    End Using

End Sub

问题是它没有保存在我的数据库中?????请帮助我,正如我之前在这个网站上所说的,整个用户实例和AttachDbFileName=方法都有缺陷,最多!Visual Studio将围绕
.mdf
文件进行复制,并且很可能,您的
插入
工作正常-但您最终看到的是错误的.mdf文件

如果您想继续使用这种方法,请尝试在
myconnection.Close()
调用上放置一个断点,然后使用SQL Server Mgmt Studio Express检查
.mdf
文件,我几乎可以肯定您的数据在那里

我认为真正的解决办法是

  • 安装SQL Server Express(您已经完成了安装)

  • 安装SQL Server Management Studio Express

  • 在SSMS Express中创建数据库,并为其指定一个逻辑名称(例如,
    MyAppDatabase

  • 使用其逻辑数据库名称连接到它(在服务器上创建时提供),不要乱动物理数据库文件和用户实例。在这种情况下,您的连接字符串类似于:

    Data Source=.\\SQLEXPRESS;Database=MyAppDatabase;Integrated Security=True
    
    其他一切都和以前一样


  • @Sray:我的下一步是将对mycommand.ExecuteOnQuery()的调用放入一个
    try…catch
    块中,查看是否发生异常,如果发生,它是什么,它告诉了什么me@Shray:此外:
    mycommand.ExecuteNonQuery()
    还有一个类型为
    int
    的返回值-它告诉您有多少行受到影响(例如插入)-您从该返回值中得到了什么值??