初识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;
       }

 

        

    


阅读更多
个人分类: ASP.NET
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭