1

I have a query like below,how can i execute it from code (in C#) ? I can run this query in sql server.

create procedure addSystemError
    @date Datetime,
    @type nvarchar(50),
    @des nvarchar(max)
as
    insert into SystemError values(@date,@type,@des)
GO


create procedure loadSystemErrorCount
    @startDate datetime,
    @endDate datetime,
    @type nvarchar(50)
as
    if(@type='All')
        select COUNT(*) from SystemError where date>=@startDate and date<@endDate
    else
        select COUNT(*) from SystemError where date>=@startDate and date<@endDate and type=@type
Go
Jesuraja
  • 3,774
  • 4
  • 24
  • 48

2 Answers2

0
using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("addSystemError", conn) { 
                       CommandType = CommandType.StoredProcedure }) {
 conn.Open();

 /* Add the parameters in command
  .    
  . 
  .
 */

 command.ExecuteNonQuery();
 conn.Close();
}
Jidheesh Rajan
  • 4,744
  • 4
  • 23
  • 29
-1

this should work:

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();

SqlDataReader reader;

cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.

sqlConnection1.Close();

Microsoft how to

user3840692
  • 273
  • 2
  • 13