C#中直接执行SQL的方法

C#中直接执行SQL的方法

查询语句:

  public static DataTable GetSelectResult()
    {
        DataTable dtResult;
        dtResult = new DataTable();
        try
        {
            using (OracleConnection oc = new OracleConnection(HttpContext.Current.Session["DBName"].ToString().Trim()))
            {
                oc.Open();
                string strGetParentSQL = @" select * fron AA
";
                OracleDataAdapter oaCmd = new OracleDataAdapter(strGetParentSQL, oc);
                //oaCmd.SelectCommand.Parameters.Add("B", OracleType.VarChar, 10).Value = B;
                oaCmd.Fill(dtResult);
                oc.Close();
            }
        }
        catch (Exception ex)
        {
           
        }
       
        return dtResult;

    }

非查询语句:

   public static void DoInsert(string A, string B)
    {
        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["SMT"];
            string strConCen = dtAEPDB.Rows[1]["String"].ToString() + ";password=" + GetPwdString(dtAEPDB.Rows[1]["Password"].ToString());
            strConCen = strConCen.Substring(0, strConCen.Length) + ";Connection Lifetime=60;Max Pool Size=50;Min Pool Size=0;Pooling=true";
            strConCen = "Data Source=" + strConCen.Substring(7);

            //using (OracleConnection oc = new OracleConnection(HttpContext.Current.Session["DBName"].ToString().Trim()))
            using (OracleConnection oc = new OracleConnection(strConCen))
            {
                oc.Open();
                string strGetParentSQL = @" INSERT INTO AA
  (A, B)
VALUES
  (:A, :B)
";

                OracleCommand oaCmd = new OracleCommand(strGetParentSQL, oc);
                oaCmd.Parameters.Add("A", OracleType.VarChar, 30).Value = A;
                oaCmd.Parameters.Add("B", OracleType.VarChar, 30).Value = B;
                oaCmd.CommandType = CommandType.Text;
                oaCmd.ExecuteNonQuery();
                oc.Close();

            }
        }
        catch (Exception ex)
        {

        }
    }

 

posted @ 2021-05-04 16:33  码农阿亮  阅读(520)  评论(0编辑  收藏  举报