ITPub博客

首页 > Linux操作系统 > Linux操作系统 > OleDbParameter在Access数据库Insert时参数不对应的怪现象[友情提示]

OleDbParameter在Access数据库Insert时参数不对应的怪现象[友情提示]

原创 Linux操作系统 作者:大可山 时间:2009-01-06 10:32:17 0 删除 编辑

protected void signup_submit_ServerClick(object sender, ImageClickEventArgs e)
    {
        //入数据库
        string connStr = EightourConfiguration.DbConnectionString;
        string loginNameValue = LoginName.Value;
        string passwordValue = Password.Value;
        string mobileValue = Mobile.Value;
        string emailValue = Email.Value;
        string sexValue = Sex.SelectedValue;
        int sex = 2;
        if (sexValue == "T")
        {
            sex = 1;
        }
        else if (sexValue == "F")
        {
            sex = 0;
        }
        string chineseNameValue = ChineseName.Value;
        string addressValue = Address.Value;
        string postcodeValue = Postcode.Value;
        string telValue = Tel.Value;

       //注意参数顺序
        string updateSql = "insert into MB_BaseInfo ([LoginName],[Password],[ChineseName],[Sex]) values (@Para_LoginName,@Para_Password,@Para_ChineseName,@Para_Sex)";
        OleDbCommand cmd = new OleDbCommand(updateSql);

       //注意参数顺序与上面一一对应
        OleDbParameter Para_LoginName = new OleDbParameter("@Para_LoginName", System.Data.OleDb.OleDbType.VarChar, 50);
        Para_LoginName.Value = loginNameValue;
        cmd.Parameters.Add(Para_LoginName);

        OleDbParameter Para_Password = new OleDbParameter("@Para_Password", System.Data.OleDb.OleDbType.VarChar, 50);
        Para_Password.Value = passwordValue;
        cmd.Parameters.Add(Para_Password);

        OleDbParameter Para_ChineseName = new OleDbParameter("@Para_ChineseName", System.Data.OleDb.OleDbType.VarChar, 50);
        Para_ChineseName.Value = chineseNameValue;
        cmd.Parameters.Add(Para_ChineseName);

        OleDbParameter Para_Sex = new OleDbParameter("@Para_Sex", System.Data.OleDb.OleDbType.Integer);
        Para_Sex.Value = sex;
        cmd.Parameters.Add(Para_Sex);
       
        int lastId = -1;

        using (OleDbConnection myConnection = new OleDbConnection(connStr))
            {
                cmd.Connection = myConnection;
                myConnection.Open();   
                OleDbTransaction trans = myConnection.BeginTransaction();
                cmd.Transaction = trans;
                try
                {
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "select @@identity";
                    lastId = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                    trans.Commit();
                }
                catch (Exception exc)
                {
                    trans.Rollback();
                    throw new Exception(exc.Message);
                }
                finally
                {
                }
            }
            if (lastId == -1) return;

            bool isSuccess = false;

       //注意参数顺序
        updateSql = "insert into MB_PersonalInfo ([BaseInfoId],[Tel],[Mobile],[Address],[Postcode],[Email]) values(@Para_BaseInfoId,@Para_Tel,@Para_Mobile,@Para_Address,@Para_Postcode,@Para_Email)";
        cmd = new OleDbCommand(updateSql);

       //注意参数顺序与上面并不一一对应
        OleDbParameter Para_BaseInfoId = new OleDbParameter("@Para_BaseInfoId", System.Data.OleDb.OleDbType.Integer);
        Para_BaseInfoId.Value = lastId;
        cmd.Parameters.Add(Para_BaseInfoId);

        OleDbParameter Para_Tel = new OleDbParameter("@Para_Tel", System.Data.OleDb.OleDbType.VarChar, 50);
        Para_Tel.Value = telValue;
        cmd.Parameters.Add(Para_Tel);

//这里现在是Postcode邮政编码,上面第三个参数是Mobile,不对应了。
        OleDbParameter Para_Postcode = new OleDbParameter("@Para_Postcode", System.Data.OleDb.OleDbType.VarChar, 50);
        Para_Postcode.Value = postcodeValue;
        cmd.Parameters.Add(Para_Postcode);

        OleDbParameter Para_Mobile = new OleDbParameter("@Para_Mobile", System.Data.OleDb.OleDbType.VarChar, 50);
        Para_Mobile.Value = mobileValue;
        cmd.Parameters.Add(Para_Mobile);

        OleDbParameter Para_Address = new OleDbParameter("@Para_Address", System.Data.OleDb.OleDbType.VarChar,255);
        Para_Address.Value = addressValue;
        cmd.Parameters.Add(Para_Address);

        OleDbParameter Para_Email = new OleDbParameter("@Para_Email", System.Data.OleDb.OleDbType.VarChar, 255);
        Para_Email.Value = emailValue;
        cmd.Parameters.Add(Para_Email);

        using (OleDbConnection myConnection = new OleDbConnection(connStr))
        {
            cmd.Connection = myConnection;
            myConnection.Open();
            OleDbTransaction trans = myConnection.BeginTransaction();
            cmd.Transaction = trans;
            try
            {
                cmd.ExecuteNonQuery();
                trans.Commit();
                isSuccess = true;
            }
            catch (Exception exc)
            {
                trans.Rollback();
                throw new Exception(exc.Message);
            }
            finally
            {
            }
        }
        if (isSuccess)
        {
            lblNotifier.Visible = true;
            lblNotifier.Text = "
你已成功注册!alert('恭喜您,注册成功!');";
        }
        else
        {
            lblNotifier.Visible = false;
        }
    }
执行上面代码时,结果发现数据库表中Mobile的值成了Postcode的值,Address的值成了Mobile的值.....乱套了。
如果将上面的相关顺序调整一下,变成一一对应,就可以了。

在SQL Server数据库中就不会涉及此类问题。看来,是ACCESS的局限。


 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14601556/viewspace-528473/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
IT人士,爱好编程的码农。

注册时间:2008-06-19

  • 博文量
    118
  • 访问量
    690994