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) { } }
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/14729739.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。