I have the following struct in C# class
public struct Employee
{
public const string EMPID = "EMP_ID";
public const string FName = "FIRST_NAME";
public const string LNAME = "LAST_NAME";
public const string DEPTID = "DEPT_ID";
}
Is there an easy way to build a string as follows
const string mainquery="INSERT INTO EMP(EMP_ID,FIRST_NAME,LAST_NAME,DEPT_ID) VALUES(:EMP_ID,:FIRST_NAME,:LAST_NAME,:DEPT_ID)"
Instead of doing as follows
and then concatenating it.
const string EMP_COLS=
EMPLOYEE.EMPID + "," +
EMPLOYEE.FNAME + "," +
EMPLOYEE.LNAME + "," +
EMPLOYEE.DEPTID;
const string EMP_Values=
EMPLOYEE.EMPID + ":" +
EMPLOYEE.FNAME + ":" +
EMPLOYEE.LNAME + ":" +
EMPLOYEE.DEPTID;
You could try something like this:
StringBuilder sb = new StringBuilder();
var fields = typeof(Employee).GetFields();
for (int i = 0; i < fields.Length; ++i)
{
sb.Append(fields[i].GetValue(new Employee()));
if (i < fields.Length - 1)
{
sb.Append(',');
}
}
string result = sb.ToString();
// The above will be "EMP_ID,FIRST_NAME,LAST_NAME,DEPT_ID"
Edit: Note that above I'm assuming you've got using directives for both System.Reflection and System.Text.
If the struct held readonly properties rather than public consts you could do it with Reflection. You could call Type.GetProperties, loop through them all and call them to get out the values and insert the values into a List<string> and then join them with string.Join(", ", myList); or something.
You'd then get the first part of the string as EMP_ID, FIRST_NAME, LAST_NAME, DEPT_ID and you'd need to make a copy of that and add the :s as needed and you'd have both parts.
you can use the string.Format(string format, object[] arg) Method as follows:
string query = string.Format("INSERT INTO EMP({0},{1},{2},{3}) VALUES(:{0},:{1},:{2},:{3})",
Employee.EMPID,
Employee.FNAME,
Employee.LNAME,
Employee.DEPTID);
Hope that helps.
You will need some kind of mapping dictionary, like here, or you could just use some ORM tool to do the mapping for you.
string[] values = new[] {
"EMP_ID",
"FIRST_NAME",
"LAST_NAME",
"DEPT_ID",
};
var columns = string.Join(",", values);
var parameters = ":" + string.Join(",:", values);
var sql = string.Format("INSERT INTO EMP({0}) VALUES({1})",
columns,
parameters);
string[] cols = typeof(Employee).GetFields()
.Select(f => f.GetValue(null).ToString()).ToArray();
Console.WriteLine(string.Format(
"INSERT INTO EMP({0}) VALUES(:{1})",
string.Join(",", cols),
string.Join(",:", cols)));
Related
I am trying to create dynamic syntax function and function syntax is like below:
MyFunction( arg1,arg2,ar3.....);
I have string like this:
str = Previousvalue.Value1,Previousvalue.Value2
Now I would like to create syntax like this in final variable :
String final = MyFunction(Previousvalue.Value1,',',Previousvalue.Value2);
str = Previousvalue.Value1,Previousvalue.Value2,Previousvalue.Value3;
String final = MyFunction(Previousvalue.Value1,',',Previousvalue.Value2,',',Previousvalue.Value3);
This is how I am trying to achieve with string.join (without using loop) but not getting how to do it and this seems like impossible to do without using loop:
final = string.Join("MyFunction(", str.Split(','));
Case 1:
Input : string str =Previousvalue.Value1,Previousvalue.Value2
Output:
string final=MyFunction(Previousvalue.Value1,',',Previousvalue.Value2,',',Previousvalue.Value3);
Case 2 :
Input : str = Previousvalue.Value1,Previousvalue.Value2,Previousvalue.Value3;
output:
String final = MyFunction(Previousvalue.Value1,',',Previousvalue.Value2,',',Previousvalue.Value3);
Case 3:
string input = " Previousvalue.Value1";
Output:
String final = Previousvalue.Value1; //No function
From what I understand, you want to generate a string like this:
"MyFunction(Previousvalue.Value1,',',Previousvalue.Value2);"
^..........^...................^....^...................^.
prefix arg1 sep arg2 suffix
or in other words
prefix = "MyFunction(";
separator = ",',',";
suffix = ");"
which can be achieved by moving the prefix and suffix out of the string.Join and using the above separator value:
string final = "MyFunction(" + string.Join(",',',", str.Split(',')) + ");";
Also instead of Split / Join you could simply use string.Replace:
string final = "MyFunction(" + str.Replace(",", ",',',") + ");";
From my understanding of the problem you want to call the MyFunction method with n string parameters, but also with string[]. You can do it like this:
public static void Main(string[] args)
{
string str = "Test1,Test2,Test3";
string test1 = MyFunction("Test1", "Test2", "Test3");
string test2 = MyFunction(str.Split(','));
}
public static string MyFunction(params string[] parameters)
{
StringBuilder sb = new StringBuilder();
foreach(var item in parameters)
{
sb.AppendLine(item);
}
return sb.ToString();
}
Try :
public string MyJoin(params string[] vars)
{
return "MyFunction(" + string.Join(",", vars) + ");";
}
I am using this code for accessing data from database and displaying it in textboxes,but i am getting whole string columns in 1st textbox ,how do i split and display in respective textboxes,i am getting this exception Index was outside the bounds of the array. at this line of code txtOption2.Text = coldata[2];
public EditQuestionMaster(int qid_value)
{
InitializeComponent();
string columns = db.GetEditQuestions(qid_value);
string[] coldata=columns.Split('$');
txtQuestion.Text = coldata[0];
txtOption1.Text = coldata[1];
txtOption2.Text = coldata[2];
txtOption3.Text = coldata[3];
txtOption4.Text = coldata[4];
}
GetEditQuestions(qid_value) Code
public string GetEditQuestions(int qid)
{
string data = "";
try
{
string sql = "select QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions where QID IN(" + qid + ") ";
cmd = new OleDbCommand(sql, acccon);
rs = cmd.ExecuteReader();
if (rs.Read())
{
data = rs[0].ToString() + "~" + rs[1].ToString() + "~" + rs[2].ToString() + "~" + rs[3].ToString() + "~" + rs[4].ToString() + "~" + rs[5].ToString() + "~" + rs[6].ToString() + "~" + rs[7].ToString() + "$";
}
}
catch (Exception err)
{
}
return data;
}
thank you in advance for any help
You appear to split the string by $ but you build the string up using ~ as the separator. You need to split the string by ~ to get the appropriate number of columns i.e.
string[] coldata = columns.Split("~")
You are seeing that error because you only have 2 items in coldata. Try debugging and view the length of the coldata array to see how many items it contains.
Change your code to use this split instead:
string[] coldata=columns.Split('~');
Looking at your code sample you just need to change:
string[] coldata=columns.Split('$');
To
string[] coldata=columns.Split('~');
As your columns are delimited by the ~ character.
Where can I find a good SQL Query builder class. I just need a simple class to build a SQL string and that is it. I will need it for C# and MySql. I really don't need anything like Linq or NHibernate. Thanks
Since Google leads me to this page,
I would suggest SqlKata, a simple but powerful SqlQuery Builder, that supports nested where conditions, subqueries and joins.
Currently it supports SqlServer, MySql and PostgreSql
var query = new Query("Users")
.LeftJoin("Countries", "Users.CountryId", "Countries.Id")
.Where("Status", "blocked")
.OrWhereIn("Id", new [] {10, 11, 12})
.OrWhere("LastLogin", ">", DateTime.UtcNow.AddMonths(-5));
Note: I am the owner of it
Difference between different compilers output
MySql: https://sqlkata.com/playground/mysql?code=var%20query%20=%20new%20Query(%22Posts%22).Limit(10).Offset(20)%3B
SqlServer: https://sqlkata.com/playground/sqlserver?code=var%20query%20=%20new%20Query(%22Posts%22).Limit(10).Offset(20)%3B
Oracle: https://sqlkata.com/playground/oracle?code=var%20query%20=%20new%20Query(%22Posts%22).Limit(10).Offset(20)%3B
I use this code..It Escapes the strings too i hope it Helps:
class Mysql
{
public static string INSERT(string INTO, NameValueCollection VALUES)
{
string queryString = "INSERT INTO " + INTO + " (";
for (int i = 0; i < VALUES.Count; i++)
{
queryString += VALUES.Keys[i] + (i + 1 == VALUES.Count ? "" : ",");
}
queryString += ") VALUES (";
for (int i = 0; i < VALUES.Count; i++)
{
queryString += Escape(VALUES[VALUES.Keys[i]]) + (i + 1 == VALUES.Count ? ("") : (","));
}
queryString += ");";
return queryString;
}
public static string DELETE(string FROM, NameValueCollection WHERE)
{
string queryString = "DELETE FROM " + FROM + " WHERE";
for (int i = 0; i < WHERE.Count; i++)
{
queryString += " " + WHERE.Keys[i] + "=" + Escape(WHERE[WHERE.Keys[i]]);
}
queryString += ";";
return queryString;
}
public static string UPDATE(string UPDATE, NameValueCollection SET, NameValueCollection WHERE)
{
string queryString = "UPDATE " + UPDATE + " SET";
for (int i = 0; i < SET.Count; i++)
{
queryString += " " + SET.Keys[i] + "=" + data.Escape(SET[SET.Keys[i]]) + (i + 1 == SET.Count ? ("") : (","));
}
queryString += " WHERE";
for (int i = 0; i < WHERE.Count; i++)
{
queryString += " " + WHERE.Keys[i] + "=" + data.Escape(WHERE[WHERE.Keys[i]]);
}
queryString += ";";
return queryString;
}
public static string SELECT(string[] SELECT, string FROM, NameValueCollection WHERE)
{
string queryString = "SELECT ";
for (int i = 0; i < SELECT.Length; i++)
{
queryString += SELECT[i] + (i + 1 == SELECT.Length ? ("") : (","));
}
queryString += " FROM " + FROM + " WHERE ";
for (int i = 0; i < WHERE.Count; i++)
{
queryString += " " + WHERE.Keys[i] + "=" + Escape(WHERE[WHERE.Keys[i]]);
}
queryString += ";";
return queryString;
}
public static string Escape(string input)
{
using (var writer = new StringWriter())
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
return writer.ToString();
}
}
}
}
You use it like this:
NameValueCollection nvc_for_SET_and_VALUES=new NameValueCollection();
NameValueCollection nvc_for_WHERE= new NameValueCollection();
nvc_for_WHERE.Add("arg1","value1");
nvc_for_WHERE.Add("AND arg2","value2");
nvc_for_WHERE.Add("OR arg2","value3");
nvc_for_SET_and_VALUES.Add("arg", "value");
nvc_for_SET_and_VALUES.Add("arg2", "value2");
string[] fieldsToSelect= { "arg1", "arg2" };
Mysql.DELETE("mytable", nvc_for_WHERE);
Mysql.INSERT("mytable", nvc_for_SET_and_VALUES);
Mysql.SELECT(fieldsToSelect, "mytable", nvc_for_WHERE);
Mysql.UPDATE("mytable", nvc_for_SET_and_VALUES, nvc_for_WHERE);
You could probably use the framework class CommandBuilder. Check out:
http://msdn.microsoft.com/en-us/library/tf579hcz.aspx
If you are using .NET 4 and don't mind using dynamics you can use Massive, created by Rob Conery, this single file Database requires no dlls, just drop the Massive.cs file and you ready to go.
You can use the Massive to build queries like this.
//grab all the products
var products = table.All();
//Or
var productsFour = table.All(columns: "ProductName as Name", where: "WHERE categoryID=#0",args: 4);
You can also run ad-hoc queries as needed:
var result = tbl.Query("SELECT * FROM Categories");
Mohammed Hadi, with DbExtensions your sample can be like this:
public static string InsertQuery(string into, NameValueCollection values)
{
var query = SQL
.INSERT_INTO(into + " (" +
String.Join(" ,", values.Keys.Cast<String>().ToArray()) +
")")
.VALUES(values.Keys.Cast<String>().Select(key => values[key]));
return query.ToString();
}
As far as i understood, a string with an # in required a set of double quotes to insert the quote in to the string?
I have tried that principle and to no avail. The following line works, but if i were to replace those strings with parameter values then i cant seem to get the correct compilation value
var node = doc.SelectSingleNode(#"//node[#label = ""Chemist Name""]/node[#label = ""John,Smith""]");
my attempt (of which i have tried several versions and ended up here, where i have now givn up !)
var node = doc.SelectSingleNode(#"//node[#label = " + ""+parentID+"" + "]/node[#label = " + ""+ name +"" + "]");
can anyone help me please?
Use single quotes:
var node = doc.SelectSingleNode
(#"//node[#label = 'Chemist Name']/node[#label = 'John,Smith']");
var node = doc.SelectSingleNode(
string.format(#"//node[#label = '{0}']/node[#label = '{1}']"
, parentID, name));
You are missing another double quote to close the string being appended and also # before each string containing "".
Try this:
var node =
doc.SelectSingleNode(#"//node[#label = """ + parentID + #"""]/node[#label = """ + name + #"""]");
var node = doc.SelectSingleNode(string.format(#"//node[#label = ""{0}""]/node[#label = ""{1}""]", parentId, name));
Write an extension method to extend string:
public static string Quote(this string input)
{
return string.Format(#"""{0}""", input);
}
And then use it as follows:
var node = doc.SelectSingleNode(#"//node[#label = " + parentID.Quote() + "]/node[#label = " + name.Quote() + "]");
Or simply:
var node = doc.SelectSingleNode(string.Format(#"//node[#label = {0}"]/node[#label = {1}"]",parentID.Quote(), name.Quote());
Here is the source code for this test:
var tags = new List<string> {"Portland", "Code","StackExcahnge" };
const string separator = " ";
tagString = tags.Aggregate(t => , separator);
Console.WriteLine(tagString);
// Expecting to see "Portland Code StackExchange"
Console.ReadKey();
Update
Here is the solution I am now using:
var tagString = string.Join(separator, tags.ToArray());
Turns out string.Join does what I need.
For that you can just use string.Join.
string result = tags.Aggregate((acc, s) => acc + separator + s);
or simply
string result = string.Join(separator, tags);
String.Join Method may be?
This is what I use
public static string Join(this IEnumerable<string> strings, string seperator)
{
return string.Join(seperator, strings.ToArray());
}
And then it looks like this
tagString = tags.Join(" ")