How do I execute an SQL statement?

前端 未结 2 438
北海茫月
北海茫月 2021-01-23 05:07

I am very new to programming, self-learnt most of it. hope someone can give me advice on the codes.

basically, the problem I am having is that my SQL statement does not

2条回答
  •  天涯浪人
    2021-01-23 06:11

    Assuming you are using SqlServer as your database:

    The SqlCommand and SqlConnection classes will be of use to you here, so I would have a look at those.

    Also Stored Procedures are a great way to go as they are more secure, and perform slightly faster than sending the query to the server. A quick google for the benefits of Stored procs will help you here

    One thing to make note of is there are different ways to execute your query. The SqlCommand class has quite a few methods for instance ExecuteNonQuery() will just run a sql command that does not return data (such as an insert or delete) whereas ExecuteScalar will execute a query that returns only one value.

    There are other options such as creating a DataTable and then using a DataAdapter to fill the table.

    I will post some examples of my database connect code, sorry it's in VB but you should be able to convert to C# easily enough.

    Firstly this is a connection using a data adapter

      Dim dt As New DataTable
    
      ' There are plenty of options you can use with the SqlConnection constructor so you can just modify this to suit your needs
      Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString())
    
      Dim cmd As New SqlCommand
      cmd.CommandType = CommandType.StoredProcedure
      cmd.CommandText = "spGetMenuItemsForTickets"
      ' parameters could be added here by doing
      ' cmd.parameters.addwithvalue("@ParamName",value)
      cmd.Connection = conn
    
      Using da As New SqlDataAdapter(cmd)
         conn.Open()
         da.Fill(dt)
         conn.Close()
      End Using
    

    This is an example of using ExecuteScalar

    Dim names As String = String.Empty

      Dim ds As New DataTable
    
      Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString())
    
      Dim cmd As New SqlCommand
      cmd.CommandType = CommandType.StoredProcedure
      cmd.CommandText = "spGetUserNamesForUpdate"
      cmd.Parameters.AddWithValue("@TicketID", TicketID)
    
      cmd.Connection = conn
    
      conn.Open()
    
      names = cmd.ExecuteScalar()
    
      conn.Close()
    

    This is an example of ExecuteNonQuery It is pretty much identical to Scalar but without the returned value

         Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString())
    
         Dim cmd As New SqlCommand
    
         cmd.CommandType = CommandType.StoredProcedure
         cmd.CommandText = "spAddFileToTicket"
    
         cmd.Parameters.AddWithValue("@ticketID", TicketID)
         cmd.Parameters.AddWithValue("@filename", Filename)
         cmd.Parameters.AddWithValue("@filePath", Filepath)
         cmd.Parameters.AddWithValue("@comments", Comment)
    
         cmd.Connection = conn
    
         conn.Open()
         cmd.ExecuteNonQuery()
         conn.Close()
    

    Also in your SQL where you do LIKE ' __ ' you will probably want to do LIKE '%__%' as the % are the wildcards, you can use one or both of them, but just doing LIKE '__' would not match a value such as ___1 or 1___ etc

提交回复
热议问题