I'm developing a .NET Core 2.1 application that does not use Entity Framework. But I'm wondering how to read the connection string into the Configuration because the usual method expects a DBContext file (which I do not have because I am note using EF).
If I were using EF, I would normally read the connection string from appsettings.json like this:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
How do I do it without the DBContext?
If you're using simple ado.net, you can't use DBContext. You open a connection and execute your query instead:
NOTE using statement is important here to ensure the connection is disposed/closed properly.
using (SqlConnection connection =
new SqlConnection(Configuration.GetConnectionString("DefaultConnection")))
{
// Create the Command and Parameter objects.
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("#pricePoint", paramValue);
// Open the connection in a try/catch block.
// Create and execute the DataReader, writing the result
// set to the console window.
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("\t{0}\t{1}\t{2}",
reader[0], reader[1], reader[2]);
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
Related
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
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:
Use Azure Functions to connect to an Azure SQL Database.
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);
}
I'm trying to write a Azure Webjob to insert a record into a Azure SQL.
Here is my code:
using System;
using System.Configuration;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("runing... "+ DateTime.Now.ToString());
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["AzureDB"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[myTest] ([CreateTime]) VALUES (GETDATE ( ))", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
At the beginning, I just had a Console.WriteLine code which worked fine but just after adding the codes related to reading the connectionstring and data it failed.
Do we need to upload the ddls?
Azure WebApps do not yet support .NET version 4.6.1. That framework version is not yet installed on the hosting VMs. It should be supported in the next couple months, but isn't yet. In the mean time, if you change your WebJob to target 4.6 it will work.
try
{
string strSQLConnString = GetConnectionString();
using (SqlConnection myConnection = new SqlConnection(strSQLConnString))
{
SqlCommand myCommand = new SqlCommand("spFortesting", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("#Param1", varParam1);
myCommand.Parameters.AddWithValue("#Param2", varParam2);
myCommand.Parameters.AddWithValue("#Param3", varParam3);
myCommand.Parameters.AddWithValue("#Param4", varParam4);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
dt = new DataTable();
if (myReader.HasRows)
{
dt.Load(myReader);
}
myReader.Close();
}
myConnection.Close();
}
}
catch (Exception ex)
{
throw ex;
}
I am getting exception like
"The exception message is 'Could not find stored procedure 'spFortesting'."
All other existing Stored Procs are accessed correctly.
Connection string is common for all other SQL calls in the application.
'spFortesting' is newly created StoredProc.
owner is 'dbo' I tried with dbo.spname as well
While I can access the Stored Proc and Tables which are newly created using SQL Mgmt Studio and the same credentials as in web.config, but not thru the code.
What could have been wrong.
Thanks in Advance,
Amit
My mistake! when i watched it very closely I found that connection strings are not matching, the connectionstring was being picked up from the other similar sounding virtual directory. System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(#"\AppName");
and my appname on the deployed server was AppNameNew for some unkown reasons, since ages. With the name AppName there was another virtual folder and its web.config was pointing towards different database.
Sorry for the inconvenience caused if any.
Cheers!!!
and a Happy new year
I'm a vb.net guy and have difficulty reading C#. I compiled the C# Dapper to a DLL and use it my app.
My main concern is I think I need to modify the source to integrate by default the Transient Fault Handling Framework for SQL Azure in each SQL query.
I can add the retry logic on the connection level because it is ont top of dapper, but not at the execute query level which is embedded in drapper class.
Anyone has done that yet ?
* UPDATE *
Does using only ReliableSqlConnection on top of Dapper call will handle a retry logic on the execute non query ?
Here is sample code of retry from MS with the transietn fault hanling
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.AzureStorage;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
using System.Data;
...
using (ReliableSqlConnection conn = new ReliableSqlConnection(connString, retryPolicy))
{
conn.Open();
IDbCommand selectCommand = conn.CreateCommand();
selectCommand.CommandText =
"UPDATE Application SET [DateUpdated] = getdate()";
// Execute the above query using a retry-aware ExecuteCommand method which
// will automatically retry if the query has failed (or connection was
// dropped).
int recordsAffected = conn.ExecuteCommand(selectCommand, retryPolicy);
}
Here is the execute part of Dapper code, same name is used but I guess it is a custom execute function
private static int ExecuteCommand(IDbConnection cnn, IDbTransaction transaction, string sql, Action<IDbCommand, object> paramReader, object obj, int? commandTimeout, CommandType? commandType)
{
IDbCommand cmd = null;
bool wasClosed = cnn.State == ConnectionState.Closed;
try
{
cmd = SetupCommand(cnn, transaction, sql, paramReader, obj, commandTimeout, commandType);
if (wasClosed) cnn.Open();
return cmd.ExecuteNonQuery();
}
finally
{
if (wasClosed) cnn.Close();
if (cmd != null) cmd.Dispose();
}
}
I would recommend wrapping the retry around Dapper, preferably by using the RetryPolicy.ExecuteAction method. That way both the OPEN call to the connection
and the command itself will be retried using the TFH retry policy:
For example:
SqlRetryPolicy.ExecuteAction(() =>
{
// Place Dapper ExecuteCommand here: e.g.
ExecuteCommand(conn, trans, ... )
});
I have implemented a SQL Server CE 4 database into a web service in C# .NET.
I have shared the database connection between the web services invocation making the connection object as static (so shared by invocations):
private static SqlCeConnection sqlConnection;
Than I have implemented different methods as the following:
lock (sqlConnection)
{
SqlCeCommand cmd = sqlConnection.CreateCommand();
tryOpenConnection();
try
{
cmd.CommandText = "SELECT * FROM STAKEHOLDER";
SqlCeDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
String toAdd = "";
toAdd = rdr.GetString(0) + "," + rdr.GetString(1);
stakeholderList.Add(toAdd);
}
}
catch (Exception e)
{
stakeholderList.Add("ERROR|" + e.Message);
}
sqlConnection.Close();
}
The question is:
Is it correct to lock the SqlCeConnection for each call?
Thanks
It should work but you should use a different object for locking rather than the connection object. Somewhere in the base hierarchy, it inherits from MarshalByRefObject, which makes it an object with weak-identity. For more info check out
http://msdn.microsoft.com/en-us/library/ms182290(v=vs.100).aspx