vb.net中

时间:2017-12-21 02:15:00

标签: sql-server vb.net stored-procedures

直接查询和存储过程之间有什么区别吗?

[下文]

我有6年的查询,因为现在我在公司工作了7个月,我在.net应用程序中使用Select *Update* delete Insert使用此查询 如果这是一个商店程序或直接查询,或者输出是相同的,我不知道它叫什么

<WebMethod()> _
    Public Function add(ByVal firstname As String, ByVal lastname As String)


        Dim con As SqlConnection
        Dim com As SqlCommand

        con = New SqlConnection(cstring)
        con.Open()
         com = New SqlCommand("SELCT * FROM NewSalestbl where Firstname = @Firstname and Lastname= @Lastname", con)

        com.Parameters.AddWithValue("@Firstname", firstname)
        com.Parameters.AddWithValue("@Lastname", lastname)
        Dim reader As SqlDataReader = com.ExecuteReader
        While reader.Read
            status = reader("Lastname").ToString
        End While

        MsgBox("Inserted")

        con.Close()

        Return status

    End Function

我想知道在创建查询时最好的程序是什么?

[下文]

我现在尝试使用此代码

    Dim con As SqlConnection
    Dim com As New SqlCommand
    con = New SqlConnection(cstring)
    con.Open()
    com.CommandText = "searchtest"
    com.CommandType = CommandType.StoredProcedure
    com.Connection = con
    com.Parameters.AddWithValue("@firstname", TextBox1.Text)
    Dim raeder As SqlDataReader
    raeder = com.ExecuteReader
    While raeder.Read
        MsgBox(raeder(1))
    End While

如果这两个例子相同或没有TIA

,任何人都可以解释这个

1 个答案:

答案 0 :(得分:1)

直接SQL很好,因为它更便携。从SQL Server跳转到另一个数据库?更改程序中的文本,您不需要数据库访问。如果您的组织不允许您创建存储过程,那也很好。 (一个人的所有数据库访问权限,另一个人的所有桌面/网络代码。)

存储过程很好,因为它已编译。对于一个简单的查询,它可能并不重要,但对于更长的多语句查询,它可能会。如果查询中出现错误,您可以在服务器上的一个位置更改它,而不会将新的可执行文件推送到十几个桌面。

答案是,测试性能两种方式,如果没有显着差异,那么你应该根据什么使维护最麻烦来选择。

我更喜欢程序,但您的情况可能会有所不同。