Any kind of help to connect with SQL Server in .Net?

0

Hello stackoverflow community,

I want to do a project in Vb .Net that connects with SQL Server, I have been told that there are classes that help me with that and that they contain functions to make calls to Stored Procedures or perform direct operations such as SELECT, INSERT or DELETE

I have searched, but I can not find those classes! I guess those classes have some special name but I do not know which is

    
asked by N. Zaldivar 22.07.2016 в 18:09
source

1 answer

1

The classes you must use to connect to SQL Server are called ADO.net

SQL Server and ADO.NET

to work with a Stored Procedure you would basically use:

Dim dt As DataTabla = New DataTable()
Using conn As New SqlConnection("connectionstring")

    Dim cmd As New SqlCommand("<storedprocedure>", conn)
    cmd.CommandType = SqlCommandType.StoredProcedure

    cmd.Parameters.AddWithValue("@param1", valor)

    Dim da As New SqlDataAdapter(cmd)
    da.Fill(dt)
End Using   

In this case I would apply to make a select since you load a datatable with the records that returns, you define the name of procedure , you assign the CommandType of the object command and you maintain parameters

To make a insert or update would be:

Dim dt As DataTabla = New DataTable()
Using conn As New SqlConnection("connectionstring")

    Dim cmd As New SqlCommand("<storedprocedure>", conn)
    cmd.CommandType = SqlCommandType.StoredProcedure

    cmd.Parameters.AddWithValue("@param1", valor)

    cmd.ExecuteNonQuery()

End Using

It's practically the same only that you execute it with the ExecuteNonQuery()

    
answered by 22.07.2016 / 18:47
source