.net设置启动窗口,数据库基本操作

本文介绍了一个.NET应用程序中如何利用Npgsql库连接并操作PostgreSQL数据库的方法。具体包括数据库连接配置、执行SQL语句获取单个整型或字符串结果、获取多条记录的数据集、执行更新操作以及调用存储过程等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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();
   }

  }
  
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值