| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1156 人关注过本帖
标题:求助,三层架构插入数据的时候老是报异常..(已解决)
只看楼主 加入收藏
jevonsjin
Rank: 1
等 级:新手上路
威 望:1
帖 子:60
专家分:0
注 册:2007-4-25
收藏
 问题点数:0 回复次数:1 
求助,三层架构插入数据的时候老是报异常..(已解决)
具体代码:
public class SqlHelper
    {
        readonly static string ConnStr = ConfigurationManager.ConnectionStrings["SqlCon"].ConnectionString;
        public static SqlCommand PrePareCommand(string sqlstr,SqlParameter[] para)
        {
            SqlConnection con = new SqlConnection(ConnStr);
            SqlCommand cmd = new SqlCommand(sqlstr,con);
            if(para!=null)
            {
                cmd.Parameters.AddRange(para);
            }
            return cmd;
        }
        public static int ExcuteNonQuery(string sqlstr,SqlParameter[] para)
        {
            SqlCommand cmd = PrePareCommand(sqlstr, para);
            

            try
            {
                cmd.Connection.Open();
                return cmd.ExecuteNonQuery();
            }
            finally
            {
                cmd.Connection.Close();
            }
        }

        public static SqlDataReader ExcuteReader(string sqlstr, SqlParameter[] para)
        {
            SqlCommand cmd = PrePareCommand(sqlstr,para);
            cmd.Connection.Open();
            SqlDataReader sdr = cmd.ExecuteReader( CommandBehavior.CloseConnection);
            return sdr;
        }
      
    }


DAL层中用下面这个方法插数据
public bool Insert(string name,string pwd,string sex,string email,string address)
        {
            string sqlStr = "insert into UserInfo (UName,UPwd,USex,UEmail,UAddress) values (@name,@pwd,@sex,@email,@address)";
            
            SqlParameter [] paras = new SqlParameter[]{
            new SqlParameter("@uname",name),
            new SqlParameter("@upwd",pwd),
            new SqlParameter("@usex",sex),
            new SqlParameter("@uemail",email),
            new SqlParameter("@uaddress",address)
            };
            if (SqlHelper.ExcuteNonQuery(sqlStr,paras) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            

        }

BLL层
public bool InsertUser(string name, string pwd, string sex, string email, string address)
        {
            return us.Insert(name,pwd, sex,email,address);
        }

页面中
 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string name = this.txtName.Text;
        string pwd = this.txtPwd.Text;
        string sex = string.Empty;
        string email = this.txtEmail.Text;
        string address = this.txtAddress.Text;
        if (this.radM.Checked)
        {
            sex = "男";
        }
        else
        {
            sex = "女";
        }
        bool flag = um.InsertUser(name, pwd, sex, email, address);

        if (flag)
        {
            
        }
        
        
    }
异常的地方是SqlHelper类中 return cmd.ExecuteNonQuery(); 说"="号附近有语法错误
try
            {
                cmd.Connection.Open();
                return cmd.ExecuteNonQuery();
            }
            finally
            {
                cmd.Connection.Close();
            }


弄了很久不知道哪里写错了
请大家看一看
谢谢

[[it] 本帖最后由 jevonsjin 于 2008-9-14 03:22 编辑 [/it]]
搜索更多相关主题的帖子: 数据 架构 
2008-09-14 03:10
jevonsjin
Rank: 1
等 级:新手上路
威 望:1
帖 子:60
专家分:0
注 册:2007-4-25
收藏
得分:0 
找到地方了原来sql语句写错了...
2008-09-14 03:22
快速回复:求助,三层架构插入数据的时候老是报异常..(已解决)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040053 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved