23 using System.Diagnostics.CodeAnalysis;
29 namespace Ntp.Analyzer.Data.Sql
41 private const string SelectSql =
42 "SELECT id, name, ip, orgId FROM host;";
44 private const string InsertSql =
45 "INSERT INTO host( id, name, ip, orgId ) VALUES( @id, @name, @ip, @orgId );";
47 private const string UpdateSql =
48 "UPDATE host SET name = @name, ip = @ip, orgId = @orgId WHERE id = @id";
50 protected override bool UseCache =>
true;
52 protected override string TableName =>
"host";
54 protected override string CreateSql =>
"CREATE TABLE host ( " +
55 " id INT NOT NULL PRIMARY KEY, " +
56 " name VARCHAR(255) NOT NULL, " +
57 " ip VARCHAR(45) NOT NULL, " +
61 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
69 Command.CommandText = PrepareInsertSql(InsertSql);
70 Command.Parameters.Add(CreateParameter(
"@id", item.
Id));
71 Command.Parameters.Add(CreateParameter(
"@name", item.
Name));
72 Command.Parameters.Add(CreateParameter(
"@ip", item.
Ip));
73 Command.Parameters.Add(CreateParameter(
"@orgId", item.
OrgId));
75 Log.SqlExecute(Command.CommandText, Command.Parameters);
76 Command.ExecuteNonQuery();
80 Log.InsertError(TableName, e);
89 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
95 Command.CommandText = PrepareSql(SelectSql);
96 Log.SqlExecute(Command.CommandText);
97 Reader = Command.ExecuteReader();
101 int id = Convert.ToInt32(Reader[
"id"]);
102 string name = Reader[
"name"].ToString();
103 string ip = Reader[
"ip"].ToString();
104 var orgId = Reader[
"orgId"] != DBNull.Value
105 ? Convert.ToInt32(Reader[
"orgId"])
107 var host =
new Host(
id, name, ip, orgId,
false);
113 Log.ReadError(TableName, e);
121 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
129 Command.CommandText = PrepareSql(UpdateSql);
130 Command.Parameters.Add(CreateParameter(
"@name", item.
Name));
131 Command.Parameters.Add(CreateParameter(
"@ip", item.
Ip));
132 Command.Parameters.Add(CreateParameter(
"@orgId", item.
OrgId));
133 Command.Parameters.Add(CreateParameter(
"@id", item.
Id));
135 Log.SqlExecute(Command.CommandText, Command.Parameters);
136 Command.ExecuteScalar();
140 Log.UpdateError(TableName, e);
OR/M mapper for table host.
HostDatabaseMapper(LogBase log)
override void ReadContent()
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
override void Update(Host item)
int Id
Gets the identifier.
override void Insert(Host item)