插入,使用更新和删除从SQL数据库中的数据vb.net? [英] Insert,update and delete data from Sql database using vb.net?

查看:262
本文介绍了插入,使用更新和删除从SQL数据库中的数据vb.net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code在数据表中插入的数据。当执行即时得到消息成功。但数据不是present在数据表中。

 进口System.Data这
进口System.Data.SqlClient的

部分公共类_Default
    继承System.Web.UI.Page
    昏暗参考译文]字符串=数据源= \ SQLEX $ P $干燥综合征; AttachDbFilename = G:\ ANDY \点Net.Practical \ SQL数据库\ inserting4 \ inserting4 \程序App_Data \ location.mdf;集成安全=真;用户实例= TRUE
    昏暗的SQL作为字符串
    昏暗CON作为新的SqlConnection
    DIM在cmd作为的SqlCommand

    保护小组的Page_Load(BYVAL发件人为对象,BYVALË作为System.EventArgs)把手Me.Load

        CON =新的SqlConnection(S)
        con.Open()

    结束小组

    保护小组的button1_Click(BYVAL发件人为对象,BYVAL E上EventArgs的)把手Button1.Click

        SQL =插入的位置(名称,街道,市)的值(@名,@街@市)
        CMD =新的SqlCommand(SQL,CON)

        cmd.Parameters.Add(@名,SqlDbType.NVarChar)。价值= TextBox1.Text
        cmd.Parameters.Add(@街,SqlDbType.NVarChar)。价值= TextBox2.Text
        cmd.Parameters.Add(@城市,SqlDbType.NVarChar)。价值= TextBox3.Text
        cmd.ExecuteNonQuery()

        MSGBOX(SUCCESS)


    结束小组
末级
 

解决方案

整个的用户实例和AttachDbFileName = 的方法是有缺陷的 - 在最好的!当在Visual Studio中运行你的应用程序,它会围绕 .MDF 文件被复制(从的App_Data 目录到输出目录 - 通常 \斌\调试 - 在这里你的应用程序运行)和最有可能,您的插入工作得很好 - 但你只是在看的错误.MDF最终文件

如果你想坚持使用这种方法,然后尝试把一个断点上的 myConnection.Close()通话 - 再检查。中密度纤维板文件与SQL Server管理工作室防爆preSS - 我几乎可以肯定你的数据是否有

真正的解决办法在我看来是将

  1. 安装SQL Server前preSS(和你已经这样做了呢)

  2. 安装SQL Server Management Studio中前preSS

  3. 创建您的 SSMS防爆preSS ,给它一个逻辑名称的数据库(如位置

  4. 连接到使用它的逻辑数据库命名为(当你在服务器上创建它提供) - 而不要与周围的物理数据库文件和用户实例。在这种情况下,您的连接字符串会是这样:

     数据源= \\ SQLEX $ P $干燥综合征;数据库=位置;集成安全性=真
     

    和其他一切都是完全和以前一样......

This is my code for inserting data in the data table. When Execute i m getting the message "SUCCESS". But the data is not present in the data table.

Imports System.Data
Imports System.Data.SqlClient

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Dim s As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=G:\ANDY\Dot Net.Practical\SQL DATA BASE\inserting4\inserting4\App_Data\location.mdf;Integrated Security=True;User Instance=True"
    Dim sql As String
    Dim con As New SqlConnection
    Dim cmd As SqlCommand

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        con = New SqlConnection(s)
        con.Open()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        sql = "insert into location(name,street,city)values(@name,@street,@city)"
        cmd = New SqlCommand(sql, con)

        cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = TextBox1.Text
        cmd.Parameters.Add("@street", SqlDbType.NVarChar).Value = TextBox2.Text
        cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = TextBox3.Text
        cmd.ExecuteNonQuery()

        MsgBox("SUCCESS")


    End Sub
End Class

解决方案

The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. Location)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=Location;Integrated Security=True
    

    and everything else is exactly the same as before...

这篇关于插入,使用更新和删除从SQL数据库中的数据vb.net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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