title: C# SQL query with user input
body: <p>I tried to make software that could be use as a database for libraries so I started to learn SQL I alredy did way to save a book into the database but I am stuck at searching in database I really dont know how to write query with user input I tried to google but cant find the answer I am stuck at this problem for a while now, please help me someone (I write my code normally in my native language but I rewrited it in english but if I forgot something to rewrite I am sorry I am in a bit of hurry when writing this).</p>
  <pre><code>try
          {
              using (connection = new SqlConnection(connectionString))
              {
                  if(connection.State == ConnectionState.Closed)
                      connection.Open();
                  using(DataTable dt = new DataTable(&quot;Search&quot;))
                  {
                      using(SqlCommand cmd = new SqlCommand(&quot;SELECT * FROM books WHERE name = @name OR name LIKE @name&quot;, connection))
                      {
                          cmd.Parameters.AddWithValue(&quot;@name&quot;, txtboxSearchName.Text);
                          cmd.Parameters.AddWithValue(&quot;author&quot;, string.Format(&quot;%{0}%&quot;, txtboxSearchAuthor.Text));
                          SqlDataAdapter adapter = new SqlDataAdapter();
                          adapter.Fill(dt);
                          dataGridView1.DataSource = dt;
                      }
                  }
              }
          }
          catch (Exception)
          {
              MessageBox.Show(&quot;Something went wrong&quot;, &quot;error&quot;, MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
  </code></pre>