• C#实现连接数据库


    C#实现连接数据库

    一、Oracle

    查询

      public static DataTable QueryData()
        {
            DataTable dtResult = new DataTable();
            try
            {
                using (OracleConnection oc = new OracleConnection(HttpContext.Current.Session["DBName"].ToString().Trim()))
                {
                    oc.Open();
                    string sql = @" SELECT * FROM DUAL
    ";
                    OracleDataAdapter oaCmd = new OracleDataAdapter(sql, oc);
                    //oaCmd.SelectCommand.Parameters.Add("fDate", OracleType.VarChar, 50).Value = DateTime.Now.ToString("yyyy/MM/dd") + " " + "00:00:00";
                    //oaCmd.SelectCommand.Parameters.Add("eDate", OracleType.VarChar, 50).Value = DateTime.Now.ToString("yyyy/MM/dd") + " " + "23:59:59";
                    oaCmd.Fill(dtResult);
                    oc.Close();
                }
            }
            catch (Exception ex)
            {
                
            }
            return dtResult;
        }

    更新

     public static void DoInser(string login_user, string login_db)
        {
            try
            {
    
                //string strDBXMLFile = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString()) + @"DB.XML";
                //DataSet dsXML = new DataSet();
                //dsXML.ReadXml(strDBXMLFile);
                //DataTable dtAEPDB = dsXML.Tables["DB_NAME"];
                //DB 链接
                string s = "Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ip地址 )(PORT =端口)))(CONNECT_DATA = (SID=SID号)(SERVER = DEDICATED)));uid = 用户名; password=密码;Connection Lifetime=60;Max Pool Size=50;Min Pool Size=0;Pooling=true";
                using (OracleConnection oc = new OracleConnection(s))
                {
                    oc.Open();
                    string sql = @" INSERT INTO 表名
      (栏位1, 栏位2, 栏位3,....)
    VALUES
      (栏位1值, 栏位2值, 栏位3值, ....)
    ";
    
                    OracleCommand oaCmd = new OracleCommand(sql, oc);
                    //oaCmd.Parameters.Add("参数", OracleType.VarChar, 30).Value = "";
                    oaCmd.CommandType = CommandType.Text;
                    oaCmd.ExecuteNonQuery();
                    oc.Close();
    
                }
            }
            catch (Exception ex)
            {
               
            }
        }

    二、SQLServer

    查询

        public static DataTable QueryData()
        {
            DataTable dtResult = new DataTable();
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(HttpContext.Current.Session["DBName"].ToString().Trim()))
                {
                    sqlConnection.Open();
                    string sql = @" SELECT * FROM DUAL
    ";
                    SqlDataAdapter sqlDa = new SqlDataAdapter(sql, sqlConnection);
                    sqlDa.SelectCommand.Parameters.Add("fDate", SqlDbType.VarChar, 50).Value = DateTime.Now.ToString("yyyy/MM/dd") + " " + "00:00:00";
                    sqlDa.SelectCommand.Parameters.Add("eDate", SqlDbType.VarChar, 50).Value = DateTime.Now.ToString("yyyy/MM/dd") + " " + "23:59:59";
                    sqlDa.Fill(dtResult);
                    sqlConnection.Close();
                }
            }
            catch (Exception ex)
            {
                
            }
            return dtResult;
        }

    更新

      public static void DoInser(string login_user, string login_db)
        {
            try
            {
    
                string s = "数据库链接";
                using (SqlConnection sqlConnection = new SqlConnection(s))
                {
                    sqlConnection.Open();
                    string sql = @" INSERT INTO 表名
      (栏位1, 栏位2, 栏位3,....)
    VALUES
      (栏位1值, 栏位2值, 栏位3值, ....)
    ";
    
                    SqlDataAdapter sqlDa = new SqlDataAdapter(sql, sqlConnection);
                    sqlDa.InsertCommand.Parameters.Add("参数", SqlDbType.VarChar, 30).Value = "";
    
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
    
                    sqlConnection.Close();
    
                }
            }
            catch (Exception ex)
            {
               
            }
        }

    工具类

        public DataTable ExecuteQuery(string sqlStr)      //用于查询;其实是相当于提供一个可以传参的函数,到时候写一个sql语句,存在string里,传给这个函数,就会自动执行。
        {
            SqlConnection con = new SqlConnection("MySqlCon");
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sqlStr;
            DataTable dt = new DataTable();
            SqlDataAdapter msda;
            msda = new SqlDataAdapter(cmd);
            msda.Fill(dt);
            con.Close();
            return dt;
        }
        public int ExecuteUpdate(string sqlStr)      //用于增删改;
        {
            SqlConnection con = new SqlConnection("MySqlCon");
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sqlStr;
            int iud = 0;
            iud = cmd.ExecuteNonQuery();
            con.Close();
            return iud;
        }

     


    技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
    我创建了一个.NET开发交流群,用于分享学习心得和讨论相关技术难题。欢迎有兴趣的小伙伴扫码入群,相互学习!

  • 相关阅读:
    Sql Server 日期推算处理,格式化处理
    自动升级功能(zt)
    绘制普通的可上下左右布局的RULER(zt)
    合并两个WORD文档,并且修改Word中标签的内容
    Sql Server 中汉字处理排序规则,全角半角
    用函数式编程技术编写优美的 JavaScript(zt)
    什么是序列化?
    日期概念理解中的一些测试
    DataSet,DataTable 不使用EXCEL组件直接保存为EXCEL
    接口测试自动化实践指南
  • 原文地址:https://www.cnblogs.com/wml-it/p/15148239.html
Copyright © 2020-2023  润新知