asp.net C# 数据库添加信息有错误 代码提示有问题

unfish 2013-02-04 12:20:18
网站后台信息添加 之前运行正常 后来添加了两个字段 visaCb
和visaTh 运行没问题 但是添加信息时提示 DBHelper.cs中 代码有问题 我把信息添加相关页面的信息帖出来 望朋友帮忙看看
1,visaAdd.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using visa.Models;
using visa.BLL;
using visa.DAL;

public partial class systemadmin_tab_visaAdd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id = Convert.ToInt32(Request.QueryString["lei"].ToString());
Td_Region region = Td_RegionManager.GetTd_RegionByRegionId(id);
Label1.Text = region.FK_Country.CouName + "-" + region.RegionName;
ViewState["regionid"] = id.ToString();
}
}

protected void BtnAdd_Click(object sender, EventArgs e)
{
Td_Visa visa = new Td_Visa();
visa.VisaName = TxtTitle.Text;
visa.VisaPrice = TxtPrice.Text;
visa.VisaCb = TxtCb.Text;
visa.VisaTh = TxtTh.Text;
visa.VisaStop = TxtStop.Text;
visa.VisaFanwei = Txtfw.Text;
visa.VisaRegionId = Convert.ToInt32(ViewState["regionid"].ToString());
visa.VisaIntro = TxtContent.Text;
visa.VisaMark = TxtMark.Text;
if (CheckBox1.Checked == true)
{
visa.VisaType = "2";
}
if (CheckBox1.Checked == false)
{
visa.VisaType = "1";
}
visa.VisaSqys = Txtsqys.Text;

visa.VisaTime = TxtTime.Text;
if (yqh1.Checked == true)
{
visa.Visayqh = yqh1.Text;
}
if (yqh2.Checked == true)
{
visa.Visayqh = yqh2.Text;
}
if (yy1.Checked == true)
{
visa.Visayy = yy1.Text;
}
if (yy2.Checked == true)
{
visa.Visayy = yy2.Text;
}
if (ms1.Checked == true)
{
visa.Visams = ms1.Text;
}
if (ms2.Checked == true)
{
visa.Visams = ms2.Text;
}
if (fy1.Checked == true)
{
visa.Visaqzf = fy1.Text;
}
if (fy2.Checked == true)
{
visa.Visaqzf = fy2.Text;
}
Td_VisaManager.AddTd_Visa(visa);
Response.Write("<script>window.location.href='visaList.aspx?lei=" + visa.VisaRegionId + "';alert('添加成功!')</script>");
}

}


2,Td_VisaManager.cs
引用
//============================================================
// 项目: visa
// 版本: 1.0
// 作者: chenchangjie
// 时间: 2012-4-19 18:34:01
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using visa.DAL;
using visa.Models;

namespace visa.BLL
{
public static partial class Td_VisaManager
{
public static int AddTd_Visa(Td_Visa td_Visa)
{
return Td_VisaService.InsertTd_Visa(td_Visa);
}

public static void DeleteTd_Visa(Td_Visa td_Visa)
{
Td_VisaService.DeleteTd_Visa(td_Visa);
}

public static void DeleteTd_VisaByVisaId(int visaId)
{
Td_VisaService.DeleteTd_VisaByVisaId(visaId);
}

public static void ModifyTd_Visa(Td_Visa td_Visa)
{
Td_VisaService.UpdateTd_Visa(td_Visa);
}

public static Td_Visa GetTd_VisaByVisaId(int visaId)
{
return Td_VisaService.SelectTd_VisaByVisaId(visaId);
}

public static IList<Td_Visa> GetTd_VisasByVisaRegionId(int visaRegionId)
{
return Td_VisaService.SelectTd_VisasByVisaRegionId(visaRegionId);
}

public static IList<Td_Visa> GetAllTd_Visas()
{
return Td_VisaService.SelectAllTd_Visas();
}
public static IList<Td_Visa> SelectAllTd_VisasBySql(string sql)
{
return Td_VisaService.SelectAllTd_VisasBySql(sql);
}

public static Td_Visa SelectOneTd_VisaBySql(string sql)
{
return Td_VisaService.SelectOneTd_VisaBySql(sql);
}

}
}


...全文
199 14 点赞 打赏 收藏 举报
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
unfish 2013-02-04
5 目录为
  • 打赏
  • 举报
回复
unfish 2013-02-04
4 DBHelper.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace visa.DAL
{
    public static class DBHelper
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        private static string connString=ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
        /// <summary>
        /// 执行sql语句并返回受影响的行数
        /// </summary>
        /// <param name="safeSql"></param>
        /// <returns></returns>
        public static int ExecuteCommand(string safeSql)
        {
            return ExecuteCommand(safeSql,null);
        }

        public static int ExecuteCommand(string sql, params SqlParameter[] values)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (values!=null)
                {
                    cmd.Parameters.AddRange(values);
                }
                conn.Open();
                result = cmd.ExecuteNonQuery();

            }
            return result;
           
        }
        /// <summary>
        /// 执行查询返回第一行第一列
        /// </summary>
        /// <param name="safeSql"></param>
        /// <returns></returns>
        public static int GetScalar(string safeSql)
        {
            return GetScalar(safeSql, null);
        }

        public static int GetScalar(string sql, params SqlParameter[] values)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (values != null)
                {
                    cmd.Parameters.AddRange(values);
                }
                conn.Open();
                result = Convert.ToInt32(cmd.ExecuteScalar());

            }
            return result;
        }

        public static SqlDataReader GetReader(string safeSql)
        {
            return GetReader(safeSql,null);
        }

        public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            if (values != null)
            {
                cmd.Parameters.AddRange(values);
            }
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        public static DataTable GetTable(string safeSql)
        {
            return GetTable(safeSql, null);
        }

        public static DataTable GetTable(string sql, params SqlParameter[] values)
        {
            DataTable dt = new DataTable();//返回值
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (values != null)
                {
                    cmd.Parameters.AddRange(values);
                }
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                conn.Open();
                da.Fill(dt);                
            }
            return dt;        
        }
    }
}
  • 打赏
  • 举报
回复
unfish 2013-02-04
3 Td_VisaService.cs
//============================================================
// 项目:		visa
// 版本: 			1.0
// 作者:			chenchangjie
// 时间: 	2012-4-26 18:17:06
//============================================================

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using visa.Models;

namespace visa.DAL
{
    public static partial class Td_VisaService
    {
        public static int InsertTd_Visa(Td_Visa td_Visa)
        {
            //定义sql语句
            string sql =
                "INSERT Td_Visa (VisaName, VisaPrice, VisaCb,VisaTh,VisaStop, VisaTime, VisaFanwei, VisaIntro, VisaRegionId, VisaMuban, VisaMark, VisaType, VisaSqys, Visayqh, Visams, Visayy, Visaqzf, VisaImage)" +
                "VALUES (@VisaName, @VisaPrice, @VisaCb,@VisaTh,@VisaStop, @VisaTime, @VisaFanwei, @VisaIntro, @VisaRegionId, @VisaMuban, @VisaMark, @VisaType, @VisaSqys, @Visayqh, @Visams, @Visayy, @Visaqzf, @VisaImage);select @@identity";
            SqlParameter[] para = new SqlParameter[]
			{  
				new SqlParameter("@VisaName", td_Visa.VisaName),
				new SqlParameter("@VisaPrice", td_Visa.VisaPrice),
                new SqlParameter("@VisaCb", td_Visa.VisaCb),
                new SqlParameter("@VisaTh", td_Visa.VisaTh),
				new SqlParameter("@VisaStop", td_Visa.VisaStop),
				new SqlParameter("@VisaTime", td_Visa.VisaTime),
				new SqlParameter("@VisaFanwei", td_Visa.VisaFanwei),
				new SqlParameter("@VisaIntro", td_Visa.VisaIntro),
				new SqlParameter("@VisaRegionId", td_Visa.VisaRegionId),
				new SqlParameter("@VisaMuban", td_Visa.VisaMuban),
				new SqlParameter("@VisaMark", td_Visa.VisaMark),
				new SqlParameter("@VisaType", td_Visa.VisaType),
				new SqlParameter("@VisaSqys", td_Visa.VisaSqys),
				new SqlParameter("@Visayqh", td_Visa.Visayqh),
				new SqlParameter("@Visams", td_Visa.Visams),
				new SqlParameter("@Visayy", td_Visa.Visayy),
				new SqlParameter("@Visaqzf", td_Visa.Visaqzf),
				new SqlParameter("@VisaImage", td_Visa.VisaImage)
			};
            //执行sql语句
            return DBHelper.GetScalar(sql, para);
        }

        public static void DeleteTd_Visa(Td_Visa td_Visa)
        {
            DeleteTd_VisaByVisaId(td_Visa.VisaId);
        }

        public static void DeleteTd_VisaByVisaId(int visaId)
        {
            //定义sql语句
            string sql = "DELETE Td_Visa WHERE VisaId = @VisaId";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@VisaId", visaId)
			};
            //执行sql语句
            DBHelper.ExecuteCommand(sql, para);
        }

        public static void UpdateTd_Visa(Td_Visa td_Visa)
        {
            //定义sql语句
            string sql =
                "UPDATE Td_Visa " +
                "SET " +
                    "VisaName = @VisaName ," +
                    "VisaPrice = @VisaPrice ," +
                    "VisaCb = @VisaCb ," +
                    "VisaTh = @VisaTh ," +
                    "VisaStop = @VisaStop ," +
                    "VisaTime = @VisaTime ," +
                    "VisaFanwei = @VisaFanwei ," +
                    "VisaIntro = @VisaIntro ," +
                    "VisaRegionId = @VisaRegionId ," +
                    "VisaMuban = @VisaMuban ," +
                    "VisaMark = @VisaMark ," +
                    "VisaType = @VisaType ," +
                    "VisaSqys = @VisaSqys ," +
                    "Visayqh = @Visayqh ," +
                    "Visams = @Visams ," +
                    "Visayy = @Visayy ," +
                    "Visaqzf = @Visaqzf ," +
                    "VisaImage = @VisaImage " +
                "WHERE VisaId = @VisaId";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@VisaId", td_Visa.VisaId),
				new SqlParameter("@VisaName", td_Visa.VisaName),
				new SqlParameter("@VisaPrice", td_Visa.VisaPrice),
                new SqlParameter("@VisaCb", td_Visa.VisaCb),
                new SqlParameter("@VisaTh", td_Visa.VisaTh),
				new SqlParameter("@VisaStop", td_Visa.VisaStop),
				new SqlParameter("@VisaTime", td_Visa.VisaTime),
				new SqlParameter("@VisaFanwei", td_Visa.VisaFanwei),
				new SqlParameter("@VisaIntro", td_Visa.VisaIntro),
				new SqlParameter("@VisaRegionId", td_Visa.VisaRegionId),
				new SqlParameter("@VisaMuban", td_Visa.VisaMuban),
				new SqlParameter("@VisaMark", td_Visa.VisaMark),
				new SqlParameter("@VisaType", td_Visa.VisaType),
				new SqlParameter("@VisaSqys", td_Visa.VisaSqys),
				new SqlParameter("@Visayqh", td_Visa.Visayqh),
				new SqlParameter("@Visams", td_Visa.Visams),
				new SqlParameter("@Visayy", td_Visa.Visayy),
				new SqlParameter("@Visaqzf", td_Visa.Visaqzf),
				new SqlParameter("@VisaImage", td_Visa.VisaImage)
			};
            //执行sql语句
            DBHelper.ExecuteCommand(sql, para);
        }

        public static Td_Visa SelectTd_VisaByVisaId(int visaId)
        {
            Td_Visa info = null;//返回值
            string sql = string.Format("SELECT * FROM Td_Visa WHERE VisaId = {0}", visaId);
            IList<Td_Visa> list = SelectTd_VisasBySql(sql);
            if (list.Count > 0)
            {
                info = list[0];
            }
            return info;
        }

        public static Td_Visa SelectOneTd_VisaBySql(string sql)
        {
            Td_Visa info = null;//返回值
            IList<Td_Visa> list = SelectTd_VisasBySql(sql);
            if (list.Count > 0)
            {
                info = list[0];
            }
            return info;
        }

        public static IList<Td_Visa> SelectAllTd_Visas()
        {
            string sql = "SELECT * FROM Td_Visa";
            return SelectTd_VisasBySql(sql);
        }

        public static IList<Td_Visa> SelectAllTd_VisasBySql(string sql)
        {
            return SelectTd_VisasBySql(sql);
        }


        public static IList<Td_Visa> SelectTd_VisasByVisaRegionId(int visaRegionId)
        {
            string sql = string.Format("SELECT * FROM Td_Visa WHERE VisaRegionId={0}", visaRegionId);
            return SelectTd_VisasBySql(sql);
        }


        private static IList<Td_Visa> SelectTd_VisasBySql(string safeSql)
        {
            return SelectTd_VisasBySql(safeSql, null);
        }

        private static IList<Td_Visa> SelectTd_VisasBySql(string sql, params SqlParameter[] values)
        {
            IList<Td_Visa> list = new List<Td_Visa>();//返回值

            DataTable table = DBHelper.GetTable(sql, values);
            foreach (DataRow row in table.Rows)
            {
                Td_Visa td_Visa = new Td_Visa();
                if (!Convert.IsDBNull(row["VisaId"]))
                {
                    td_Visa.VisaId = (int)row["VisaId"];
                }
                if (!Convert.IsDBNull(row["VisaName"]))
                {
                    td_Visa.VisaName = (string)row["VisaName"];
                }
                if (!Convert.IsDBNull(row["VisaPrice"]))
                {
                    td_Visa.VisaPrice = (string)row["VisaPrice"];
                }
                if (!Convert.IsDBNull(row["VisaCb"]))
                {
                    td_Visa.VisaCb = (string)row["VisaCb"];
                }
                if (!Convert.IsDBNull(row["VisaTh"]))
                {
                    td_Visa.VisaTh = (string)row["VisaTh"];
                }
                if (!Convert.IsDBNull(row["VisaStop"]))
                {
                    td_Visa.VisaStop = (string)row["VisaStop"];
                }
                if (!Convert.IsDBNull(row["VisaTime"]))
                {
                    td_Visa.VisaTime = (string)row["VisaTime"];
                }
                if (!Convert.IsDBNull(row["VisaFanwei"]))
                {
                    td_Visa.VisaFanwei = (string)row["VisaFanwei"];
                }
                if (!Convert.IsDBNull(row["VisaIntro"]))
                {
                    td_Visa.VisaIntro = (string)row["VisaIntro"];
                }
                if (!Convert.IsDBNull(row["VisaRegionId"]))
                {
                    td_Visa.VisaRegionId = (int)row["VisaRegionId"];
                }
                if (!Convert.IsDBNull(row["VisaMuban"]))
                {
                    td_Visa.VisaMuban = (string)row["VisaMuban"];
                }
                if (!Convert.IsDBNull(row["VisaMark"]))
                {
                    td_Visa.VisaMark = (string)row["VisaMark"];
                }
                if (!Convert.IsDBNull(row["VisaType"]))
                {
                    td_Visa.VisaType = (string)row["VisaType"];
                }
                if (!Convert.IsDBNull(row["VisaSqys"]))
                {
                    td_Visa.VisaSqys = (string)row["VisaSqys"];
                }
                if (!Convert.IsDBNull(row["Visayqh"]))
                {
                    td_Visa.Visayqh = (string)row["Visayqh"];
                }
                if (!Convert.IsDBNull(row["Visams"]))
                {
                    td_Visa.Visams = (string)row["Visams"];
                }
                if (!Convert.IsDBNull(row["Visayy"]))
                {
                    td_Visa.Visayy = (string)row["Visayy"];
                }
                if (!Convert.IsDBNull(row["Visaqzf"]))
                {
                    td_Visa.Visaqzf = (string)row["Visaqzf"];
                }
                if (!Convert.IsDBNull(row["VisaImage"]))
                {
                    td_Visa.VisaImage = (string)row["VisaImage"];
                }

                td_Visa.FK_VisaRegion = Td_RegionService.SelectTd_RegionByRegionId((int)row["VisaRegionId"]); //FK

                list.Add(td_Visa);
            }
            return list;
        }
    }
}
  • 打赏
  • 举报
回复
unfish 2013-02-04
各位 谢啦……
  • 打赏
  • 举报
回复
应该是逗号写成全角的了
  • 打赏
  • 举报
回复
黄瓜黄瓜 2013-02-04
引用 10 楼 unfish 的回复:
引用 8 楼 lyq8376 的回复:sql是怎么写的? C# code?123 string sql = "INSERT Td_Visa (VisaName, VisaPrice, VisaCb,VisaTh,VisaStop, VisaTime, VisaFanwei, VisaIntro, VisaRegionId, VisaMuban……
中文逗号
  • 打赏
  • 举报
回复
看下是不是sql语句把英文逗号写成中文逗号了
  • 打赏
  • 举报
回复
unfish 2013-02-04
引用 8 楼 lyq8376 的回复:
sql是怎么写的?
 string sql =
                "INSERT Td_Visa (VisaName, VisaPrice, VisaCb,VisaTh,VisaStop, VisaTime, VisaFanwei, VisaIntro, VisaRegionId, VisaMuban, VisaMark, VisaType, VisaSqys, Visayqh, Visams, Visayy, Visaqzf, VisaImage)" +
                "VALUES (@VisaName, @VisaPrice, @VisaCb,@VisaTh,@VisaStop, @VisaTime, @VisaFanwei, @VisaIntro, @VisaRegionId, @VisaMuban, @VisaMark, @VisaType, @VisaSqys, @Visayqh, @Visams, @Visayy, @Visaqzf, @VisaImage);select @@identity";
  • 打赏
  • 举报
回复
引用 7 楼 unfish 的回复:
错误信息提示 DBHelper.cs
sql语句出问题了,检查下sql语句
  • 打赏
  • 举报
回复
sql是怎么写的?
  • 打赏
  • 举报
回复
unfish 2013-02-04
错误信息提示 DBHelper.cs
  • 打赏
  • 举报
回复
unfish 2013-02-04
引用 4 楼 oONukeOo 的回复:
代码这么多,逐条看是很费劲的,你应该把异常提示信息粘贴出来,最起码先锁定出错位置。
我想贴出来呢,系统提示“回复提交失败”
  • 打赏
  • 举报
回复
cheng2005 2013-02-04
这代码看着都累,你写的时候不感觉累吗?呵呵
  • 打赏
  • 举报
回复
萨拉嘿 2013-02-04
代码这么多,逐条看是很费劲的,你应该把异常提示信息粘贴出来,最起码先锁定出错位置。
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2013-02-04 12:20
社区公告

让您成为最强悍的C#开发者