0

So I have this Azure function that works, and an Azure SQL DB with some data. But I cannot find a decent example to get data from the DB into the function. Surely, crafting a query string and SQLCommand.BeginExecuteReader/EndExecuteReader is not the preferred way, right? LINQtoSQL perhaps?

Thanks, Bezz

3
  • There are a lot of different way to get data from sql using C#/.Net, what are you asking for ? What have you tried so far ? Commented Jun 13, 2018 at 1:49
  • Thx for your response. I'm sure there are different ways to do this. At the moment, I'm doing just a proof of concept, so I guess I'm looking for a "Hi, Scott Hanselman here and in this five minute video, I'm going to show you how to do basic CRUD work from your Azure function to Azure SQL DB for our record store test app."-kinda answer :-) Commented Jun 13, 2018 at 7:52
  • Hmmmm, maybe it's just as simple as this: 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(); Commented Jun 13, 2018 at 8:51

2 Answers 2

1

You are free to use any .NET Data Access library that's available for other types of applications: ADO.NET, Entity Framework, Dapper etc.

A simple example:

Sign up to request clarification or add additional context in comments.

2 Comments

Thx for your response. I'm sure there are different ways to do this. At the moment, I'm doing just a proof of concept, so I guess I'm looking for a "Hi, Scott Hanselman here and in this five minute video, I'm going to show you how to do basic CRUD work from your Azure function to Azure SQL DB for our record store test app."-kinda answer :-)
Sorry Mikhail, overlooked your link. Thanks for that, but that's just what I meant by crafting a query string. I followed SQLCommand and ended up with BeginExecuteReader. Maybe my knowledge of SQLCommand is too limited and there might be a simpler way to do a simple select query.
0

Apparently, it was quite simple. This code did the trick. Although I'm not completely happy with the fact that I'm crafting a query string. For now, it will do.

#r "System.Configuration"
#r "System.Data"

using System.Configuration;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    var str = ConfigurationManager.ConnectionStrings["sqldb_connection"].ConnectionString;
    var caterers = new List<string>();

    using (SqlConnection conn = new SqlConnection(str))
    {
         conn.Open();

         SqlCommand cmd = new SqlCommand();
         SqlDataReader reader;

         cmd.CommandText = "SELECT * FROM Caterers";
         cmd.Connection = conn;

         reader = cmd.ExecuteReader();

         if (reader.HasRows)
         {
             while (reader.Read())
             {
                 caterers.Add(reader.GetString(1));
             }
         }

         conn.Close();
    }
    return req.CreateResponse(HttpStatusCode.OK, caterers);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.