初识ASP.NET

利用VS自带数据库连接:

引用命名空间using System.Data.SqlClient;

 public void DeleteDb()
        {
            SqlConnection sqlcon = new SqlConnection(@"Data Source=.\sqlexpress;Integrated Security=True");
            sqlcon.Open();
            string sql = "delete FROM Experiment_Content";
            SqlCommand sqlCmd = new SqlCommand(sql, sqlcon);
            SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
            DataTable table = new DataTable();
            sda.Fill(table);
            // this.dataGridView1.DataSource = table;
            sqlcon.Close();

        }
        public void Insert(string sql0)
        {
            SqlConnection sqlcon = new SqlConnection(@"Data Source=.\sqlexpress;Integrated Security=True");
            sqlcon.Open();
            //string sql = "insert into Experiment (Exp_Id,Tran_Range,Node_Num,Client_retio,Transtraction_Num,Npush) values(4,1,1,1,1,1)";
            string sql = sql0;
            SqlCommand sqlCmd = new SqlCommand(sql, sqlcon);
            SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
            DataTable table = new DataTable();
            sda.Fill(table);
            //this.dataGridView1.DataSource = table;
            sqlcon.Close();
        }


1、 数据库连接的2种方式:
(1)Data Source=;Initial Catalog=;Integrated  Security=True 不需要通过sa和密码直接登录页面
(2)<add name="take_out_system" connectionString="Data Source=USER62-PC\SQLEXPRESS;Initial Catalog=_TakeOutSystem;User Id=sa;Password=123456;"></add>
2.明码变成隐码:在相应的textbox里面多加入一行属性 TextMode="Password"(其中Password为textbox的Id)

3.类里面的一些命名规则。变量一般为_id ,_userName (下划线+小写。。后面的英语单词

大写)
方法名:一般大写字母开头

4.学会使用数据库的增删改查4条基本的语句:

:   string sql = "insert into category([name]) values(@caName)";

 

 public bool AddComment(Comment c)
       {
           string sql = "insert into comment(newsid,[content],userip) values(@newsID,@content,@userIP)";
           SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@newsID",c.NewsId),new SqlParameter("@content",c.Content),new SqlParameter("@userIP",c.UserIP)};
           if (sqlhelper.ExecuteNonQuery(sql, paras,CommandType.Text) > 0)
           {
               return true;
           }
           return false;
       }

 

insert into [table] ([字段]) values(@a)

: string sql = "Delete from category whereid=@id";

 

 public bool Delete(int id)
       {
           string sql = "delete comment where
id=@id";
           SqlParameter[] paras = new SqlParameter[] {new SqlParameter("@id",id) };
           if (sqlhelper.ExecuteNonQuery(sql,paras,CommandType.Text)>0)
           {
               return true;
           }
           return false;
       }

 

:string sql = "update category set [name]=@caName whereid=@caId";

 

 public bool Update(Category ca)
        {
            bool flag = false;
            string sql = "update category set [name]=@caName where
id=@caId";
            SqlParameter[] paras = new SqlParameter[]
            {
            new SqlParameter("@caName",ca.Name),new SqlParameter("@caId",ca.Id)
            };

            if (sqlhelper.ExecuteNonQuery(sql, paras,CommandType.Text) > 0)
            {
                flag = true;
            }
            return flag;
        }

 

 

:string sql = "select * from category whereid=@caid";

 

   public DataTable SelectByNewsID(int newsId)
       {
           DataTable dt = new DataTable();
           string sql = "select * from comment where
newsid=@newsId";
           SqlParameter[] paras=new SqlParameter[]{new SqlParameter("@newsId",newsId)};
           dt=sqlhelper.ExecuteQuery(sql, paras,CommandType.Text);
           return dt;
       }

 

        

    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值