Для пункта меню «Запрос без параметра» ввести код процедуры обработки события
string strconn = string.Format("Server={0};Port={1};User ID={2};Password={3};Database={4}", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text);
using (NpgsqlConnection cnn = new NpgsqlConnection(strconn))
{
cnn.Open();
string stsql = "";
stsql = "%" + textBox6.Text + "%";
stsql = "select * from \"Жанры\" where \"Имя\" like \'" + stsql + "\';";
using (NpgsqlCommand myCommand = new NpgsqlCommand(stsql, cnn))
{
using (NpgsqlDataReader dr = myCommand.ExecuteReader())
{
listBox1.Items.Clear();
while (dr.Read())
{
listBox1.Items.Add(Convert.ToString(dr[1]));
}
}
}
}
Задача 3. Вызвать хранимую процедуру (функцию) с параметром и вернуть ее результат в клиентское приложение
1. Проверить, что на сервере в БД video есть функция my1(int,int). Если функции нет, то создать:
CREATE OR REPLACE FUNCTION my1(integer, integer)
RETURNS integer AS
$BODY$declare s integer;
begin
select $1+$2 into s;
return s;
end;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION my1(integer, integer)
OWNER TO postgres;
эта функция складывает два числа
2. Ввести текст процедуры обработки события выбора пункта подменю «Скалярная функция с параметром»:
string strconn = string.Format("Server={0};Port={1};User ID={2};Password={3};Database={4}", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text);
|
|
using (NpgsqlConnection cnn = new NpgsqlConnection(strconn))
{
cnn.Open();
try
{
NpgsqlCommand myCommand = new NpgsqlCommand("my1", cnn);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new NpgsqlParameter());
myCommand.Parameters[0].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Integer;
myCommand.Parameters[0].Value = 4;
myCommand.Parameters.Add(new NpgsqlParameter());
myCommand.Parameters[1].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Integer;
myCommand.Parameters[1].Value = 5;
myCommand.Parameters.Add(new NpgsqlParameter());
myCommand.Parameters[2].NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Integer;
myCommand.Parameters[2].Direction = ParameterDirection.Output;
Object result = myCommand.ExecuteScalar();
MessageBox.Show(Convert.ToString(myCommand.Parameters[2].Value));
// MessageBox.Show(Convert.ToString(result));
}
finally
{
cnn.Close();
}
}