数据库的增加表

数据库增加

一.在DAL层中分别在页面SQLHelper和CategoryDAO中输入以下代码

  1. 在SQLHelper中输入:

/// <summary>

        /// 该方法执行传入的增删改SQL语句

        /// </summary>

        /// <param name="sql">要执行的SQL语句</param>

        /// <returns>返回更新的记录数</returns>

        public int ExecuteNonQuery(string sql)

        {

            //连接数据库

            string connStr = @"server=SIKU;database=newssystem;uid=sa;pwd=123456";

            SqlConnection conn = new SqlConnection(connStr );

            conn.Open();

            SqlCommand cmd = new SqlCommand(sql ,conn );

           //通过此语句执行SQL语句

            int res = cmd.ExecuteNonQuery();

            conn.Close();

            return res;

        }

2.在CategoryDAO中输入:

   public  class CategoryDAO

    {

        private SQLHelper sqlhelper = null;

   public bool Insert(string caName)

        {

            bool flag = false;

            string sql = "insert into category(name) values('" + caName + "')";

            SQLHelper sqlhelper = new SQLHelper();

            int res = sqlhelper.ExecuteNonQuery(sql);

            if (res > 0)

            {

                flag = true;

            }

            return flag;

        }

}

二.           在Web层中输入:

1.           先拉入Textbox和Button

2.           Button事件

   protected void Button1_Click(object sender, EventArgs e)

    {

        string caName = TextBox1.Text;

        bool b=new CategoryDAO().Insert(caName );

       Response.Write(b )

}

注:1.Category类一定要Public

       2。一定要New一下,才能点出类使用下面的方法

       3。内容通过TEXTBOX传入

 

posted @ 2013-04-16 16:56  水库  阅读(309)  评论(0编辑  收藏  举报