关于在Asp.net下使用Oracle.DataAccess.dll(Oracle10g)执行参数化Update无效的解决方案之一...
本文共 1689 字,大约阅读时间需要 5 分钟。
- using Oracle.DataAccess.Client;
- public static String ConnectionString = "Data Source=orcl;User ID=hr;Password=hr;";
-
- public static int Update(MacInfo model)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("update MacInfo set ");
- strSql.Append("Mac_Address=:Mac_Address,");
- strSql.Append("Mac_Status=:Mac_Status");
- strSql.Append(" where MacInfo_ID=:MacInfo_ID");
- //strSql.Append("Mac_Address='"+model.Mac_Address+"',");//拼凑Update字符串是可以执行成功的
- //strSql.Append("Mac_Status="+model.Mac_Status);
- //strSql.Append(" where MacInfo_ID='"+model.MacInfo_ID+"'");
- //这里注意,strSql中的Update语句参数顺序为:Mac_Address、Mac_Status、MacInfo_ID,所以下面OracleParameter
- //也必须按这个顺序来声明、赋值
- OracleParameter[] parameters = {
- new OracleParameter(":Mac_Address", OracleDbType.Varchar2,17),
- new OracleParameter(":Mac_Status", OracleDbType.Int32,4),
- new OracleParameter(":MacInfo_ID", OracleDbType.Varchar2,17)};
- parameters[0].Value = model.Mac_Address;
- parameters[1].Value = model.Mac_Status.ToString();
- parameters[2].Value = model.MacInfo_ID;
-
- int result = ExecuteCommand(strSql.ToString(), CommandType.Text,parameters);
- return result;
- }
- public static int ExecuteCommand(string queryString, CommandType commandType, params OracleParameter[] param)
- {
- int flag = 0;
- using(OracleConnection con = new OracleConnection(ConnectionString))
- {
- OracleCommand command = new OracleCommand(queryString, con);
- command.CommandType = commandType;
- if(param != null)
- {
- for(int i = 0; i < param.Length; i++)
- {
- command.Parameters.Add(param[i]);
- }
- }
- try
- {
- command.Connection.Open();
- flag = command.ExecuteNonQuery();
- }
- catch(Exception ex)
- {
- throw ex;
- }
- finally
- {
- command.Connection.Close();
- command.Dispose();
- command = null;
- }
- }
- return flag;
- }
转载地址:http://dmwdx.baihongyu.com/