Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karlospn/10616ffaffbc664f6410538ae177c60f to your computer and use it in GitHub Desktop.
Save karlospn/10616ffaffbc664f6410538ae177c60f to your computer and use it in GitHub Desktop.
#r "System.Data"
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.Threading.Tasks;
using System.Configuration;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
public static void Run(string myQueueItem, TraceWriter log)
{
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
var query ="INSERT INTO dbo.Users (Id, Username, Password) " +
"VALUES (@Id, @Username, @Password) ";
var user = JsonConvert.DeserializeObject<User>(myQueueItem);
using(SqlConnection cn = new SqlConnection(cnnString))
using(SqlCommand cmd = new SqlCommand(query, cn))
{
// define parameters and their values
cmd.Parameters.Add("@Id", SqlDbType.UniqueIdentifier).Value = user.Id;
cmd.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = user.Username;
cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = user.Password;
// open connection, execute INSERT, close connection
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
public class User
{
public Guid Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment