static void Main()
{
System.Windows.Forms.Application.Run(new IndexForm());
}
using System;
using System.Data;
using Npgsql;
using System.Collections;
namespace 数据库基本操作
{
/// <summary>
/// DBUtil 的摘要说明。
/// </summary>
public class DBUtil
{
private Npgsql.NpgsqlConnection Conn;
private Npgsql.NpgsqlCommand cmd;
private System.Data.CommandType commandType;
public DBUtil()
{
Conn = Connection;
cmd = new NpgsqlCommand();
cmd.Connection = Conn;
}
private NpgsqlConnection Connection
{
get
{
NpgsqlConnection conn = new NpgsqlConnection("Server=192.168.90.5;Port=5432;UserId=postgres;Password=postgres;Database=Cherry;");
return conn;
}
}
//返回单个整型
public int GetInt(string sqlStr)
{
Conn.Open();
NpgsqlCommand command = new NpgsqlCommand(sqlStr, Conn);
int result;
try
{
NpgsqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
result = Convert.ToInt32(command.ExecuteScalar());
}
else{
result = -1;
}
}
catch
{
Conn.Close();
throw;
}
finally
{
Conn.Close();
}
return result;
}
//返回单个整型 记录不存在 return -2
public int GetInt2(string sqlStr)
{
Conn.Open();
NpgsqlCommand command = new NpgsqlCommand(sqlStr, Conn);
int result;
try
{
NpgsqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
result = Convert.ToInt32(command.ExecuteScalar());
}
else
{
result = -2;
}
}
catch
{
Conn.Close();
throw;
}
finally
{
Conn.Close();
}
return result;
}
// 返回单个字符串
public string GetString(string sqlStr)
{
Conn.Open();
NpgsqlCommand command = new NpgsqlCommand(sqlStr, Conn);
string result;
try
{
NpgsqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
result = (string)(command.ExecuteScalar());
}
else
{
result = "10";
}
}
catch
{
Conn.Close();
throw;
}
finally
{
Conn.Close();
}
return result;
}
//返回多条记录
public System.Data.DataSet GetResultList(string sqlStr)
{
//定义数据集对象
System.Data.DataSet dst = new System.Data.DataSet();
string strErrInfo="";
Npgsql.NpgsqlDataAdapter SqlDtAdpt = new NpgsqlDataAdapter();
Conn.Open();
NpgsqlCommand command = new NpgsqlCommand(sqlStr, Conn);
try
{
SqlDtAdpt.SelectCommand = command ;
command.CommandText = sqlStr;
command.CommandType = commandType;
SqlDtAdpt.Fill(dst);
}
catch(System.Exception ex)
{
strErrInfo ="错误提示:获取数据信息失败!" + "/n" + ex.Message;
Conn.Close();
}
finally
{
if (strErrInfo!="")
System.Windows.Forms.MessageBox.Show(strErrInfo,"提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
Conn.Close();
command = null;
SqlDtAdpt = null;
}
return dst;
}
//取系统当前时间
public String GetCurrentDay()
{
Conn.Open();
NpgsqlCommand command = new NpgsqlCommand("SELECT to_char(CURRENT_DATE,'yyyy-mm-dd')", Conn);
string result;
try
{
NpgsqlDataReader dr = command.ExecuteReader();
result = (string)command.ExecuteScalar();
}
catch
{
Conn.Close();
throw;
}
finally
{
Conn.Close();
}
return result;
}
//执行更新
public int InsertOrUpdate(string sqlStr)
{
Conn.Open();
NpgsqlCommand command = new NpgsqlCommand(sqlStr, Conn);
Int32 rowsaffected;
try
{
rowsaffected = command.ExecuteNonQuery();
}
catch
{
Conn.Close();
throw;
}
finally
{
Conn.Close();
}
return rowsaffected;
}
//调用存储过程 --由于业务太复杂,事务不好控制,所以直接调用存储过程
public void callProcedure ( string pname ,int a , int b)
{
Conn.Open();
try
{
NpgsqlCommand command = new NpgsqlCommand(pname, Conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
command.Parameters.Add(new NpgsqlParameter("b", DbType.Int32));
//Console.WriteLine("----------------------"+a);
command.Parameters[0].Value = a;
command.Parameters[1].Value = b;
Object result = command.ExecuteScalar();
Console.WriteLine(result);
}
finally
{
Conn.Close();
}
}
}
}