SQL Server 2008 R2 stored procedure does not work (visual C #)

I searched and tried different things last week or so, and my problem may be specific to finding an answer via Google.

If I run this query in SQL Server Management Studio and replace @zoekterm with '%something%' , it works fine and returns the result I want. But when I call the same procedure from C #, it returns nothing.

Is this a mistake or am I just so stupid?

Here's the code of the stored procedure and function in C # (I know that I should have used the switch case ...)

Stored Procedure:

 -- ============================================= -- Author: Daan -- Create date: -- Description: -- ============================================= ALTER PROCEDURE [dbo].[quick_bedrijf] -- Add the parameters for the stored procedure here @zoekterm varchar(100) = 0 AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SELECT bedrijf.bedrijf_nr, bedrijf.zoeknaam, bedrijf.plaats FROM bedrijf WHERE zoeknaam LIKE @zoekterm AND NIETactief = 0 ORDER BY bedrijf.zoeknaam, bedrijf.plaats END 

WITH#:

 private void snel_zoek2() { listView1.Items.Clear(); con.Open(); if (type == 1) { command1 = new SqlCommand("quick_project", con); colnum = 5; } else if (type == 2) { command1 = new SqlCommand("quick_bedrijf", con); colnum = 3; } else if (type == 3) { command1 = new SqlCommand("quick_persoon", con); colnum = 4; } command1.CommandType = CommandType.StoredProcedure; SqlParameter zoekterm = command1.Parameters.Add("@zoekterm", SqlDbType.VarChar, 100); zoekterm.Direction = ParameterDirection.Input; //command1.Parameters.Add(new SqlParameter("@zoekterm", SqlDbType.VarChar)).Value = " '%zee%'";// + textBox2.Text.ToString()+ zoekterm.Value = "'%"+textBox2.Text.ToString()+"%'"; // MessageBox.Show(zoekterm.Value.ToString()); SqlDataAdapter adapt = new SqlDataAdapter(); DataTable dt = new DataTable(); adapt.SelectCommand = command1; adapt.Fill(dt); dataGridView1.BindingContext = new BindingContext(); dataGridView1.DataSource = dt; con.Close(); } 
+4
source share
1 answer

You do not put quotation marks in the parameter (this means that it means a literal); it should be:

 zoekterm.Value = "%"+textBox2.Text+"%"; 

It does not currently work, because if the text is "abc", it searches for a line that starts and ends with a single quote and includes "abc". In terms of SQL, you asked:

 LIKE '''%abc%''' 
+5
source

Source: https://habr.com/ru/post/1339144/


All Articles