E
engahmedbarbary
Guest
Problem
How to make function return sqlparamters and full insert statement with sqlparamters ?
meaning i need to return two things
full statement insert
INSERT INTO master_table(id, branch_id, name, address, phone) VALUES(@id, @branch_id, @name, @address, @phone);
and sql paramters
@id, @branch_id, @name, @address, @phone
static void Main(string[] args)
{
string JsonData = File.ReadAllText("D:\\1.json");
JObject jo = JObject.Parse(JsonData);
JToken m = jo["master"];
string connectionstring = "Server=AHMEDSALAH-PC\\SQL2014;Database=Atum;User Id=sa;Password=abc123;"; //change connection string
using (SqlConnection connection = new SqlConnection(connectionstring))
{
using (SqlCommand command = new SqlCommand(JsonHelper.GetInsertStatement(m), connection))
{
connection.Open();
List<SqlParameter> lsp = JsonHelper.GetSqlParams(jo["master"]);
foreach (SqlParameter sqp in lsp)
command.Parameters.Add(sqp);
}
}
command return full statement
lsp return SQL parameters
how to return both this is actually my question
i need code under main function put in another function return two things above
full statement insert and SQL parameters .
so that how to write function using code under main and return two thing
1- full statement insert
INSERT INTO master_table(id, branch_id, name, address, phone) VALUES(@id, @branch_id, @name, @address, @phone);
2- and sql paramters
@id, @branch_id, @name, @address, @phone
details of code above
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ReadJsonApp
{
public static class JsonHelper
{
public static string GetInsertStatement(JToken mastertoken)
{
return string.Format("INSERT INTO {0}({1}) VALUES({2});",
mastertoken["table"],
GetFieldParameterNames(mastertoken),
GetFieldParameterNames(mastertoken, false));
}
static string GetFieldParameterNames(JToken mastertoken, bool fieldOnly = true)
{
string p = fieldOnly ? string.Empty : "@";
return string.Concat(string.Join(", ", mastertoken["keys"].Cast<JProperty>().Select(jp => p + jp.Name)),
", ", string.Join(", ", mastertoken["fields"].Cast<JProperty>().Select(jp => p + jp.Name)));
}
public static List<SqlParameter> GetSqlParams(JToken mastertoken)
{
List<SqlParameter> para = new List<SqlParameter>();
foreach (JToken jt in mastertoken["keys"])
para.Add(new SqlParameter("@" + jt.ToObject<JProperty>().Name, jt.First));
foreach (JToken jt in mastertoken["fields"])
para.Add(new SqlParameter("@" + jt.ToObject<JProperty>().Name, jt.First));
return para;
}
}
}
code work perfect but remaining to write code under main to return two things above
Continue reading...
How to make function return sqlparamters and full insert statement with sqlparamters ?
meaning i need to return two things
full statement insert
INSERT INTO master_table(id, branch_id, name, address, phone) VALUES(@id, @branch_id, @name, @address, @phone);
and sql paramters
@id, @branch_id, @name, @address, @phone
static void Main(string[] args)
{
string JsonData = File.ReadAllText("D:\\1.json");
JObject jo = JObject.Parse(JsonData);
JToken m = jo["master"];
string connectionstring = "Server=AHMEDSALAH-PC\\SQL2014;Database=Atum;User Id=sa;Password=abc123;"; //change connection string
using (SqlConnection connection = new SqlConnection(connectionstring))
{
using (SqlCommand command = new SqlCommand(JsonHelper.GetInsertStatement(m), connection))
{
connection.Open();
List<SqlParameter> lsp = JsonHelper.GetSqlParams(jo["master"]);
foreach (SqlParameter sqp in lsp)
command.Parameters.Add(sqp);
}
}
command return full statement
lsp return SQL parameters
how to return both this is actually my question
i need code under main function put in another function return two things above
full statement insert and SQL parameters .
so that how to write function using code under main and return two thing
1- full statement insert
INSERT INTO master_table(id, branch_id, name, address, phone) VALUES(@id, @branch_id, @name, @address, @phone);
2- and sql paramters
@id, @branch_id, @name, @address, @phone
details of code above
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ReadJsonApp
{
public static class JsonHelper
{
public static string GetInsertStatement(JToken mastertoken)
{
return string.Format("INSERT INTO {0}({1}) VALUES({2});",
mastertoken["table"],
GetFieldParameterNames(mastertoken),
GetFieldParameterNames(mastertoken, false));
}
static string GetFieldParameterNames(JToken mastertoken, bool fieldOnly = true)
{
string p = fieldOnly ? string.Empty : "@";
return string.Concat(string.Join(", ", mastertoken["keys"].Cast<JProperty>().Select(jp => p + jp.Name)),
", ", string.Join(", ", mastertoken["fields"].Cast<JProperty>().Select(jp => p + jp.Name)));
}
public static List<SqlParameter> GetSqlParams(JToken mastertoken)
{
List<SqlParameter> para = new List<SqlParameter>();
foreach (JToken jt in mastertoken["keys"])
para.Add(new SqlParameter("@" + jt.ToObject<JProperty>().Name, jt.First));
foreach (JToken jt in mastertoken["fields"])
para.Add(new SqlParameter("@" + jt.ToObject<JProperty>().Name, jt.First));
return para;
}
}
}
code work perfect but remaining to write code under main to return two things above
Continue reading...