-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (46 loc) · 1.92 KB
/
Program.cs
File metadata and controls
54 lines (46 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
namespace SQLProject
{
static class Program
{
class GrabParams : IFormattable
{
SqlParameter Pr;
public GrabParams(SqlParameter P) { this.Pr = P; }
public string ToString(string format, IFormatProvider formatProvider)
{
if (!string.IsNullOrEmpty(format))
{
Pr.SqlDbType = (SqlDbType)Enum.Parse(typeof(SqlDbType), format, true);
}
return Pr.ParameterName;
}
}
static SqlCommand NewSQLCommand(this SqlConnection connection, FormattableString Query)
{
SqlParameter[] Pr = Query.GetArguments().Select((Arg, Pos) => new SqlParameter($"@p{Pos}", Arg)).ToArray();
object[] formatArguments = Pr.Select(x => new GrabParams(x)).ToArray();
string command = string.Format(Query.Format, formatArguments);
SqlCommand cmd = new SqlCommand(command, connection);
cmd.Parameters.AddRange(Pr);
return cmd;
}
static void Main(string[] args)
{
SqlConnection cn = new SqlConnection();
int age = 18;
string Name = "Yassine", table = "TablePersonne", CIN = "D883756";
SqlCommand Cmd = cn.NewSQLCommand($"select * from {table} where CIN={CIN:varchar} age > {age:int} And name like %{Name}%");
Console.WriteLine("The old Query:\tselect * from {table} where CIN={CIN} age > {age:int} And name like %{Name:nvarchar}%");
Console.WriteLine($"The new Query:\t\t{Cmd.CommandText}");
Console.WriteLine("Parameters:");
foreach (SqlParameter P in Cmd.Parameters)
{
Console.WriteLine($"Name={P.ParameterName}\t\tValue:{P.Value}\t\tType:{P.SqlDbType}");
}
}
}
}