• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#oracle参数传递的多种方式留着复习

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

ORA-01036 非法的变量名/编号,(解决)

博客分类:
SQL 

下边的代码就会造成  ORA-01036 非法的变量名/编号

C#代码  
  1. cmd.CommandText = "SELECT * FROM kk.kkyh WHERE id = @comboBox1 and password = @textBox1 ";  
  2.   
  3. cmd.Parameters.Add("@comboBox1", OracleType.VarChar).Value = comboBox1.Text;  
  4. cmd.Parameters.Add("@textBox1", OracleType.VarChar).Value = textBox1.Text;  

 

把SQL中的@改成:,把.Add中的@去掉,就正常了

 

C#代码  
  1. cmd.CommandText = "SELECT * FROM kk.kkyh WHERE id = :comboBox1 and password = :textBox1 ";  
  2.   
  3. cmd.Parameters.Add("comboBox1", OracleType.VarChar).Value = comboBox1.Text;  
  4. cmd.Parameters.Add("textBox1", OracleType.VarChar).Value = textBox1.Text;  

 

黑色头发  http://heisetoufa.iteye.com

 

 

===============================================================

 

最近写程序,用EnterpriseLibrary往Oracle中插入数据,出现错误ORA-01036:非法的变量名/编号,代码如下:
DbConnection m_con = g_db.CreateConnection();
m_con.Open();
DbCommand m_com = g_db.GetSqlStringCommand("insert into USER_DEPARTMENT(DEPARTNAME,PARENTDEPID) values(@departname,@parentdepid)");
            g_db.AddInParameter(m_com, "@departname", DbType.String, this.TextBox1.Text);
            g_db.AddInParameter(m_com, "@parentdepid", DbType.String, this.TreeView1.SelectedNode.Value);
g_db.ExecuteNonQuery(m_com);
 m_con.Close();
若换成
DbCommand m_com = g_db.GetSqlStringCommand("insert into USER_DEPARTMENT(DEPARTNAME,PARENTDEPID) values('" + this.TextBox1.Text + "','" + this.TreeView1.SelectedNode.Value + "')");
则没有问题,上网查也没找到解决办法,经过一番探索,终于找到原因了,Oracle中好象不支持@,把@换成:就可以了,代码如下:
DbConnection m_con = g_db.CreateConnection();
m_con.Open();
DbCommand m_com = g_db.GetSqlStringCommand("insert into USER_DEPARTMENT(DEPARTNAME,PARENTDEPID) values(:departname,:parentdepid)");
            g_db.AddInParameter(m_com, ":departname", DbType.String, this.TextBox1.Text);
            g_db.AddInParameter(m_com, ":parentdepid", DbType.String, this.TreeView1.SelectedNode.Value);
g_db.ExecuteNonQuery(m_com);
 m_con.Close();

另外,Oracle中如果不用begin...end,SQL语句中不要有分号(;)。

 

 

========================================================

 

publicbool ModifyDrawBill(MDrawBill mBill)
{
StringBuilder strSQLText
=new StringBuilder();
strSQLText.Append(
"update DrawBill set ");
OracleParameter[] param
=
{
new OracleParameter("PKID", OracleType.Int32),
new OracleParameter("StationId", OracleType.VarChar),
new OracleParameter("BillBegin", OracleType.VarChar),
new OracleParameter("BillEnd", OracleType.VarChar),
new OracleParameter("SumCount", OracleType.Int32),
new OracleParameter("DrawDate", OracleType.DateTime),
new OracleParameter("IsConfirm", OracleType.Int32),
new OracleParameter("AppUser", OracleType.NVarChar),
new OracleParameter("CheckUser", OracleType.NVarChar),
new OracleParameter("Memo", OracleType.NVarChar)
};
//@@lwd 20110803
param[0].Value = mBill.PKID;
if (mBill.StationId !=null)
{
strSQLText.Append(
"StationId=:StationId,");
param[
1].Value = mBill.StationId;
}
if (mBill.BillBegin !=null)
{
strSQLText.Append(
"BillBegin=:BillBegin,");
param[
2].Value = mBill.BillBegin;
}
if (mBill.BillEnd !=null)
{
strSQLText.Append(
"BillEnd=:BillEnd,");
param[
3].Value = mBill.BillEnd;
}
if (mBill.SumCount !=null)
{
strSQLText.Append(
"SumCount=:SumCount,");
param[
4].Value = mBill.SumCount;
}
if (mBill.DrawDate !=null)
{
strSQLText.Append(
"DrawDate=:DrawDate,");
param[
5].Value = mBill.DrawDate;
}
if (mBill.IsConfirm !=null)
{
strSQLText.Append(
"IsConfirm=:IsConfirm,");
param[
6].Value = mBill.IsConfirm;
}
if (mBill.AppUser !=null)
{
strSQLText.Append(
"AppUser=:AppUser,");
param[
7].Value = mBill.AppUser;
}
if (mBill.CheckUser !=null)
{
strSQLText.Append(
"CheckUser=:CheckUser,");
param[
8].Value = mBill.CheckUser;
}
if (mBill.Memo !=null)
{
strSQLText.Append(
"Memo=:Memo,");
param[
9].Value = mBill.Memo;
}
if (strSQLText.ToString().EndsWith(","))
{
strSQLText
= strSQLText.Remove(strSQLText.ToString().Length -1, 1);

}
strSQLText.Append(
" where PKID=:PKID ");
return OracleHelper.ExecuteNonQuery(OracleHelper.Connection_String, CommandType.Text, strSQLText.ToString(), param) >0;//提示“RA-01036: 非法的变量名/编号” }

 

==================================================

 

 

string sql = " select rownum as   logrow,Id,UserId,UserLogCategoryId,SystemLogCategoryId,Time,ReadT,Title,Content,Permission from LogT where UserId=:UserId and Title like :Title ";
if (Title == null) Title = "";
Title = "%" + Title + "%";
OracleParameter[] paras = new OracleParameter[] { new OracleParameter(":UserId", UserId) , new OracleParameter(":Title", Title)};


======================================================================================================

oracle中错误ORA-01036: 非法的变量名/编号

 

 C#

 public Result AddUser(UserEntity user)
       {
           Result result = new Result(true);
           DbCommand dbCmd = db.GetSqlStringCommand("PK_TY_USER.INS_USER");
           db.AddInParameter(dbCmd, "id", DbType.Int32, user.Id);
           db.AddInParameter(dbCmd, "names", DbType.String, user.Name);
           db.AddInParameter(dbCmd, "sex", DbType.String, user.Sex);
           db.AddInParameter(dbCmd, "nav", DbType.String, user.Native_Place);
           db.AddInParameter(dbCmd, "birday", DbType.DateTime, user.Birthday);
           db.AddInParameter(dbCmd, "age", DbType.Int32, user.Age);
           try
           {
               db.ExecuteNonQuery(dbCmd);
           }
           catch (Exception ex)
           {

               result.Success = false;
               result.Error = ex;
               throw ex;
           }

           return result;
       }

 

oracle

 

 procedure INS_USER(id in integer,names in varchar2,sex in varchar2,nav in varchar2,birday in date,age in integer) is
                  begin
                       insert into ty_user values(id,names,sex,nav,birday,age);
                  end;

 

oracle中测试能过

 

==========================================================================

最近写程序时,往Oracle中插入数据,出现错误ORA-01036:非法的变量名/编号,代码如下:

DbConnection conn = GetCon();
            conn.Open();
            DbCommand cmd = conn.CreateCommand();
            string sqlrt= "insert into T_User(Id,Name,Password,E_Mail) VALUES(@id,@name,@password,@email)";

                cmd.CommandText = sqlrt;
                cmd.Parameters.Add(new SqlParameter("@id", Convert.ToInt32(txtId.Text)));
                cmd.Parameters.Add(new SqlParameter("@name", txtName.Text));
                cmd.Parameters.Add(new SqlParameter("@password", txtPassword.Text));
                cmd.Parameters.Add(new SqlParameter("@email", txtEmail.Text));
            int num = cmd.ExecuteNonQuery();
            if (num > 0)
            {
                Response.Write("插入成功");
                DataLoad();
            }

解决方案是:

          DbConnection conn = GetCon();
            conn.Open();
            DbCommand cmd = conn.CreateCommand();
            string sqlrt= "insert into T_User(Id,Name,Password,E_Mail) VALUES(:id,:name,:password,:email)";

              cmd.Parameters.Add(new OracleParameter(":id", Convert.ToInt32(txtId.Text)));
                cmd.Parameters.Add(new OracleParameter(":name", txtName.Text));
                cmd.Parameters.Add(new OracleParameter(":password", txtPassword.Text));
                cmd.Parameters.Add(new OracleParameter(":email", txtEmail.Text));

在Oracle数据库中,参数替换不能使用"@",要使用":",将程序中的@替换成就可以解决这个问题了
 
==============================================================

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#学习文件和注册表操作发布时间:2022-07-10
下一篇:
C#创建INI文件,写入并可读取。----转载发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap