177 lines
7.5 KiB
C#
177 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Configuration;
|
|
using VNCServerList.Models;
|
|
|
|
namespace VNCServerList.Services
|
|
{
|
|
public class DatabaseService
|
|
{
|
|
private readonly string _connectionString;
|
|
|
|
public DatabaseService()
|
|
{
|
|
_connectionString = Properties.Settings.Default.VNCServerDB;
|
|
InitializeDatabase();
|
|
}
|
|
|
|
private void InitializeDatabase()
|
|
{
|
|
using (var connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
// VNC_ServerList 테이블이 존재하는지 확인
|
|
string checkTableSql = @"
|
|
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='VNC_ServerList' AND xtype='U')
|
|
CREATE TABLE [dbo].[VNC_ServerList](
|
|
[User] [varchar](50) NOT NULL,
|
|
[IP] [varchar](20) NOT NULL,
|
|
[Category] [varchar](20) NULL,
|
|
[Description] [varchar](100) NULL,
|
|
[Password] [varchar](20) NULL,
|
|
[Argument] [varchar](50) NULL,
|
|
CONSTRAINT [PK_VNC_ServerList] PRIMARY KEY CLUSTERED
|
|
(
|
|
[User] ASC,
|
|
[IP] ASC
|
|
)
|
|
)";
|
|
|
|
using (var command = new SqlCommand(checkTableSql, connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<VNCServer> GetAllServers()
|
|
{
|
|
var servers = new List<VNCServer>();
|
|
|
|
using (var connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
string sql = "SELECT * FROM VNC_ServerList ORDER BY [User]";
|
|
|
|
using (var command = new SqlCommand(sql, connection))
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
servers.Add(new VNCServer
|
|
{
|
|
User = reader["User"].ToString(),
|
|
IP = reader["IP"].ToString(),
|
|
Category = reader["Category"] == DBNull.Value ? null : reader["Category"].ToString(),
|
|
Description = reader["Description"] == DBNull.Value ? null : reader["Description"].ToString(),
|
|
Password = reader["Password"] == DBNull.Value ? null : reader["Password"].ToString(),
|
|
Argument = reader["Argument"] == DBNull.Value ? null : reader["Argument"].ToString()
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return servers;
|
|
}
|
|
|
|
public VNCServer GetServerByUserAndIP(string user, string ip)
|
|
{
|
|
using (var connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
string sql = "SELECT * FROM VNC_ServerList WHERE [User] = @User AND [IP] = @IP";
|
|
|
|
using (var command = new SqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@User", user);
|
|
command.Parameters.AddWithValue("@IP", ip);
|
|
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
return new VNCServer
|
|
{
|
|
User = reader["User"].ToString(),
|
|
IP = reader["IP"].ToString(),
|
|
Category = reader["Category"] == DBNull.Value ? null : reader["Category"].ToString(),
|
|
Description = reader["Description"] == DBNull.Value ? null : reader["Description"].ToString(),
|
|
Password = reader["Password"] == DBNull.Value ? null : reader["Password"].ToString(),
|
|
Argument = reader["Argument"] == DBNull.Value ? null : reader["Argument"].ToString()
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public bool AddServer(VNCServer server)
|
|
{
|
|
using (var connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
string sql = @"
|
|
INSERT INTO VNC_ServerList ([User], [IP], [Category], [Description], [Password], [Argument])
|
|
VALUES (@User, @IP, @Category, @Description, @Password, @Argument)";
|
|
|
|
using (var command = new SqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@User", server.User);
|
|
command.Parameters.AddWithValue("@IP", server.IP);
|
|
command.Parameters.AddWithValue("@Category", (object)server.Category ?? DBNull.Value);
|
|
command.Parameters.AddWithValue("@Description", (object)server.Description ?? DBNull.Value);
|
|
command.Parameters.AddWithValue("@Password", (object)server.Password ?? DBNull.Value);
|
|
command.Parameters.AddWithValue("@Argument", (object)server.Argument ?? DBNull.Value);
|
|
|
|
return command.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool UpdateServer(VNCServer server)
|
|
{
|
|
using (var connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
string sql = @"
|
|
UPDATE VNC_ServerList
|
|
SET [Category] = @Category, [Description] = @Description,
|
|
[Password] = @Password, [Argument] = @Argument
|
|
WHERE [User] = @User AND [IP] = @IP";
|
|
|
|
using (var command = new SqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@User", server.User);
|
|
command.Parameters.AddWithValue("@IP", server.IP);
|
|
command.Parameters.AddWithValue("@Category", (object)server.Category ?? DBNull.Value);
|
|
command.Parameters.AddWithValue("@Description", (object)server.Description ?? DBNull.Value);
|
|
command.Parameters.AddWithValue("@Password", (object)server.Password ?? DBNull.Value);
|
|
command.Parameters.AddWithValue("@Argument", (object)server.Argument ?? DBNull.Value);
|
|
|
|
return command.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool DeleteServer(string user, string ip)
|
|
{
|
|
using (var connection = new SqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
string sql = "DELETE FROM VNC_ServerList WHERE [User] = @User AND [IP] = @IP";
|
|
|
|
using (var command = new SqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@User", user);
|
|
command.Parameters.AddWithValue("@IP", ip);
|
|
return command.ExecuteNonQuery() > 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |