Scripts/PostgreSQL.cs
Станислав Хозяенок df6e2fe069 first commit
2025-04-07 10:57:03 +07:00

68 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Npgsql;
public class PostgreSQL : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Postgres();
}
// Update is called once per frame
void Update()
{
}
public static void Postgres()
{
string connectionString =
"Port = 5433;" +
"Server=192.168.0.157;" +
"Database=unity_db;" +
"User ID=unity_db;" +
"Password=unity_db;";
// IDbConnection dbcon; ## CHANGE THIS TO
NpgsqlConnection dbcon;
dbcon = new NpgsqlConnection(connectionString);
dbcon.Open();
//IDbCommand dbcmd = dbcon.CreateCommand();## CHANGE THIS TO
NpgsqlCommand dbcmd = dbcon.CreateCommand();
// requires a table to be created named employee
// with columns firstname and lastname
// such as,
// CREATE TABLE employee (
// firstname varchar(32),
// lastname varchar(32));
string sql =
"SELECT firstname, lastname " +
"FROM employee";
dbcmd.CommandText = sql;
//IDataReader reader = dbcmd.ExecuteReader(); ## CHANGE THIS TO
NpgsqlDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
string FirstName = (reader.IsDBNull(0)) ? "NULL" : reader.GetString(0).ToString();
string LastName = (reader.IsDBNull(1)) ? "NULL" : reader.GetString(1).ToString();
//string LastName = (string)reader.GetString(1);
Debug.Log("FIO:" + FirstName + " " + LastName);
//Console.WriteLine();
}
// clean up
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
}