How can I call a SQL function in C#?

前端 未结 4 686
醉酒成梦
醉酒成梦 2021-01-05 12:36

I have created a function in SQL, now I need to use that function in my C# application.

I tried using something like this, but it seems I\'m doing it wrong since I\'

4条回答
  •  独厮守ぢ
    2021-01-05 12:51

    Your SQL is a bit off, it should be:

      string query = string.Format("select * from dbo.Function1({0},{1});", int.Parse(e.CurrentRow.Cells["CodeMeli"].Value.ToString()),1);
    

    You might want to use SqlParameter-objects to prevent sql injections:

      string query = "select * from dbo.Function1(@pa1,@par2);";
      cmd.Parameters.Add("@par1", SqlDbType.Int).Value = int.Parse(e.CurrentRow.Cells["CodeMeli"].Value.ToString());  
      cmd.Parameters.Add("@par2", SqlDbType.Int).Value = 1;
    

提交回复
热议问题