Skip to content

extention for .net core to send raw sql also to wark well eith stored views and procedures

Notifications You must be signed in to change notification settings

gheith3/RawSqlExtention

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

this eextention for .net core to work directly with database using database context, its allow to send sql for create/update/delete and select, also its work well with stored views and procedures

How its work!

after install plugin ..

  • create class model that present row of return data
      public class Student
        {
            public int ID { get; set; }
            public string LastName { get; set; }
            public string FirstMidName { get; set; }
            public DateTime EnrollmentDate { get; set; }
        }
  • select data from databse table
    var sql = "SELECT ID, LastName, FirstMidName, EnrollmentDate FROM Student";
    List<Student> students = await _context.ExecuteRawQueryAsync<Student>(sql);
  • send parameters with query
    var sql = "SELECT ID, LastName, FirstMidName, EnrollmentDate FROM Student WHERE Id = @StudentID";
     object[] parameters =
            {
                new SqlParameter("@StudentID", SqlDbType.Int)
                {
                    Direction = ParameterDirection.Input,
                    Value = 4
                }
    };
    List<Student> students = await _context.ExecuteRawQueryAsync<Student>(sql, parameters);

. to insert/update/delete row

    var sql = "INSERT INTO dbo.Student(LastName, FirstMidName, EnrollmentDate)
                            VALUES(@LastName, @FirstName, @CreatedAt)";

   object[] parameters = {
                new SqlParameter("@LastName", SqlDbType.NVarChar)
                {
                    Direction = ParameterDirection.Input,
                    Value = "San"
                },
                new SqlParameter("@FirstName", SqlDbType.NVarChar)
                {
                    Direction = ParameterDirection.Input,
                    Value = "Nami"
                },
                new SqlParameter("@CreatedAt", SqlDbType.DateTime2)
                {
                    Direction = ParameterDirection.Input,
                    Value = DateTime.Now
                }
   };
   await _context.ExecuteRawQueryAsync<object>(sql, parameters);
  • to call stored view
  • to call stored procedure

any suggestions for improvement are welcome <link> : https://twitter.com/gheith3

About

extention for .net core to send raw sql also to wark well eith stored views and procedures

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages