首页 > 数据库 >c# 简单明了,连oracle实体类 以及 操作 增删改查

c# 简单明了,连oracle实体类 以及 操作 增删改查

时间:2023-02-12 11:11:15浏览次数:47  
标签:实体类 OracleCommand string c# cmd 改查 SQLString new conn

class HelperDB
    {
        private static string connString = "User ID=用户名;Password=密码;" +
                                            "Data Source=(DESCRIPTION = " +
                                            "(ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)" +
                                          "(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = 实例名)))";
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="SQLString"></param>
        /// <returns>DataTable</returns>
        public static DataTable ExecuteDataTable(string SQLString)
        {
            OracleConnection conn = new OracleConnection(connString);
            try
            {
                conn.Open();
                MessageBox.Show(conn.State.ToString());
                string sql = string.Format(SQLString);
                OracleCommand cmd = new OracleCommand(sql, conn);
                DataTable dt = new DataTable();
                OracleDataAdapter da = new OracleDataAdapter(cmd);
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(conn.State.ToString());
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 增改删操作
        /// </summary>
        /// <param name="SQLString"></param>
        /// <returns></returns>
        public static int ExecuteSql(string SQLString)
        {
            using (OracleConnection connection = new OracleConnection(connString))
            {
                using (OracleCommand cmd = new OracleCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (OracleException E)
                    {
                        connection.Close();
                        throw new Exception(E.Message);
                    }
                }
            }
        }

 

标签:实体类,OracleCommand,string,c#,cmd,改查,SQLString,new,conn
From: https://www.cnblogs.com/zmz5107/p/17113429.html

相关文章