Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72253
  • 博文数量: 42
  • 博客积分: 2025
  • 博客等级: 大尉
  • 技术积分: 437
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-03 12:17
文章分类

全部博文(42)

文章存档

2010年(12)

2009年(30)

我的朋友

分类:

2009-11-22 20:53:10

数据库的连接操作

using MySql.Data.MySqlClient;

private MySqlCommand mycomm;

    private MySqlDataReader dr;

    private string strconn = ConfigurationManager.AppSettings["Mysqlstudb"];

private MySqlConnection myco;

<appSettings>

<add key="Mysqlstudb" value="User Id=root;Host=localhost;Database=studb;Password=root;Port=3307"/>   

appSettings>

 

数据库打开操作

方法1

string Username = TextBox1.Text;

string Pwd = TextBox2.Text;

myco = new MySqlConnection(strconn);

myco.Open();

string sql = "select * from uinfo where UNAME='" + Username + "' and PASWD='" + Pwd + "'";

mycomm = new MySqlCommand(sql, myco);

dr = mycomm.ExecuteReader();

while (dr.Read())

{

Session["Username"] = dr["uname"].ToString();

Label3.Text = Session["Username"].ToString();

}

mycomm.Dispose();

myco.Close();

 

方法2

string Username = TextBox1.Text;

string Pwd = TextBox2.Text;

myco = new MySqlConnection(strconn);

myco.Open();

string sql = "select * from uinfo where UNAME='" + Username + "' and PASWD='" + Pwd + "'";

MySqlDataAdapter myAdapter = new MySqlDataAdapter(sql, myco);

DataSet myDataSet = new DataSet();

DataView m_DataView = new DataView();

myAdapter.Fill(myDataSet);

m_DataView = myDataSet.Tables[0].DefaultView;

GridView1.DataSource = m_DataView;

GridView1.DataBind();

myAdapter.Dispose();

myco.Close();

注意:可以这样做:

myAdapter.Fill(myDataSet,"a");

m_DataView = myDataSet.Tables["a"].DefaultView;

其中"a"是表名。

 

 

函数的调用

protected void Page_Load(object sender, EventArgs e)

{

get_week_data();

}

private void get_week_data()

{

函数体

}

 

存储过程的调用

string title = TextBox1.Text;

string sort =DropDownList1.SelectedValue;

string content=Editor1.Text ;

string time = CodefanCalendar1.Text;

mycomm = new MySqlCommand("ProcTrad", myco);

mycomm.CommandType = CommandType.StoredProcedure;

MySqlParameter[] para = new MySqlParameter[]{

              new MySqlParameter("@title_",MySqlDbType.VarChar ,50),

              new MySqlParameter("@sort_",MySqlDbType.VarChar ,20),

              new MySqlParameter("@time_",MySqlDbType.DateTime),

              new MySqlParameter("@content_",MySqlDbType.MediumText)

            };

para[0].Value =title;

para[1].Value =sort;

para[2].Value =time;

para[3].Value =content;

mycomm.Parameters.AddRange(para);

mycomm.ExecuteNonQuery();

如果是只有一个参数,那么这样做:

MySqlParameter para = new MySqlParameter("@title_", MySqlDbType.VarChar, 50);

mycomm.Parameters.Add(para);

 

调用母版

MasterPageFile="~/Admin/AdminPage.master"

 

 

调用CSS样式

<link rel="stylesheet" href="../Css/css.css" type="text/css" />

CSS样式文件中带.的:<table class="contentTab" border="0" cellpadding="0" cellspacing="1">

在源文件中用class调用,如:

<tr class="contentC">

<td colspan="2" valign="top">

td>

tr>

CSS样式文件中带#的:#footer.black {color:black;}

在源文件中用id调用,如:

id="footer.black"

 

 

 

调用框架

<fieldset style ="width :98%"><legend >表头名legend>

fieldset>

 

 

使用下载组件的方法

右击“工具箱”,“选择项”,“浏览”,把dll文件添加进来,然后把下载组件的文件放在站点根目录下。

 

存储过程的写法

CREATE DEFINER=`root`@`localhost` PROCEDURE `ProcTrad`( in title_ varchar(50),

                                      in sort_ varchar(20),

                                      in time_ datetime,

                                      in content_ MEDIUMTEXT)

BEGIN

insert into logtable(title,sort,time,content)

values(title_,sort_,time_,content_);

END

 

其它

DropDownList1.SelectedValue

Editor1.Text

CodefanCalendar1.Text

定义一些数据常用操作

private MySqlCommand mycomm;

private MySqlDataReader dr;

private string strconn = ConfigurationManager.AppSettings["Mysqlstudb"];

private MySqlConnection myco;

 

 

DateList显示数据

编辑模板---拖入Label控件用于显示数据---编辑绑定

把其中的“属性”绑定字段,如 Eval("UNAME")

myco = new MySqlConnection(strconn);

myco.Open();

string sql = "select * from uinfo";

MySqlDataAdapter myAdapter = new MySqlDataAdapter(sql, myco);

DataSet ds = new DataSet();

myAdapter.Fill(ds);

DataList1.DataSource = ds.Tables[0];

DataList1.DataBind();

 

Eval页面传参

页面链接

<asp:HyperLink ID="HyperLink1"  runat="server" NavigateUrl='<%#"~/user/dsaf.aspx?IDUSE="+Eval("IDUSE")%>' Target=_blank >查看asp:HyperLink>

接受页

myco = new MySqlConnection(strconn);

myco.Open();

string IDUSE=Request.QueryString["IDUSE"];

string sql = "select * from uinfo where IDUSE='"+IDUSE+"'";

mycomm = new MySqlCommand(sql, myco);

dr = mycomm.ExecuteReader();

while (dr.Read())

{

Label1.Text = dr["UNAME"].ToString();

}

mycomm.Dispose();

myco.Close();

 

 

 

用户登录并显示自己写的文章:

登录页:从uinfo表中验证用户名和密码。

登录页面传递uinfo表中的unameuinfo表中的IDUSEcha.aspx页面

string Username = TextBox1.Text;

string Pwd = TextBox2.Text;

myco = new MySqlConnection(strconn);

myco.Open();

string sql = "select * from uinfo where UNAME='" + Username + "' and PASWD='" + Pwd + "'";

mycomm = new MySqlCommand(sql, myco);

dr = mycomm.ExecuteReader();

while (dr.Read())

{

Session["Username"] = dr["uname"].ToString();

Session["IDUSE"] = dr["IDUSE"].ToString();

}

mycomm.Dispose();

myco.Close();

Response.Redirect("cha.aspx");

 

cha.aspx页面显示uinfo表中的uname(用户名)

以及在Datalist1中显示此用户logtable表中对应的数据。

uinfo.IDUSElogtable.IDUSE关联。

Label4.Text = Session["Username"].ToString();

string IDUSE_=Session["IDUSE"].ToString();

myco = new MySqlConnection(strconn);

myco.Open();

string sql = "select * from logtable where IDUSE= '" + IDUSE_ + "'";

MySqlDataAdapter myAdapter = new MySqlDataAdapter(sql, myco);

DataSet ds = new DataSet();

myAdapter.Fill(ds);

DataList1.DataSource = ds.Tables[0];

DataList1.DataBind();

 

并在cha.aspx页面设置HyperLink1(查看)的NavigateUrl"dsaf.aspx?IDRizhi="+Eval("IDRizhi")

传递IDRzhi

 

 

 

查询页面

dsaf.aspx为接收页面

Request.QueryString接收IDRzhi值,并从logtable表中取出对应文章内容。

myco = new MySqlConnection(strconn);

myco.Open();

string IDRizhi_ = Request.QueryString["IDRizhi"];

string sql = "select * from logtable where IDRizhi='" + IDRizhi_ + "'";

mycomm = new MySqlCommand(sql, myco);

dr = mycomm.ExecuteReader();

while (dr.Read())

{

Label1.Text = dr["Title"].ToString();

Label2.Text = dr["Content"].ToString();

}

mycomm.Dispose();

myco.Close();

 

 

 

修改页面

并在cha.aspx页面设置HyperLink2(修改)的NavigateUrl"RizhiUpdata.aspx?IDRizhi="+Eval("IDRizhi")

传递IDRzhi

数据库中存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `RizhiUpdata`(

                                         in Title_ varchar(50),

                                         in Content_ MEDIUMTEXT,

                                         in IDRizhi_ INT)

BEGIN

update logtable set Title=Title_,Content=Content_ where IDRizhi=IDRizhi_;

END $$

HiddenField控件用于接收session

Page_Load

if (!IsPostBack)

{          

myco = new MySqlConnection(strconn);

myco.Open();

string IDRizhi_ = Request.QueryString["IDRizhi"];

HiddenField1.Value = IDRizhi_;

string sql = "select * from logtable where IDRizhi='" + IDRizhi_ + "'";

mycomm = new MySqlCommand(sql, myco);

dr = mycomm.ExecuteReader();

while (dr.Read())

{

TextBox1.Text = dr["Title"].ToString();

Editor1.Text = dr["Content"].ToString();

}

mycomm.Dispose();

myco.Close();

}

 

protected void Button1_Click(object sender, EventArgs e)

{

string Title=TextBox1.Text;

string content = Editor1.Text;

int IDRizhi =System.Convert.ToInt32(HiddenField1.Value);

myco = new MySqlConnection(strconn);

myco.Open();

mycomm = new MySqlCommand("RizhiUpdata", myco);

mycomm.CommandType = CommandType.StoredProcedure;

MySqlParameter[] para = new MySqlParameter[]{

new MySqlParameter("@Title_",MySqlDbType.VarChar ,50),

new MySqlParameter("@Content_",MySqlDbType.MediumText),

new MySqlParameter("@IDRizhi_",MySqlDbType.Int32,11)

};

para[0].Value = Title;

para[1].Value = content;

para[2].Value = IDRizhi;

mycomm.Parameters.AddRange(para);

mycomm.ExecuteNonQuery();

mycomm.Dispose();

myco.Close();

}

 

 

 

删除文章

cha.aspx页面中,在DataList1数据控件中添加一LinkButton控件

设置LinkButton控件的CommandName属性为Delete

在源文件中的DataList中添加一些属性:

OnDeleteCommand="DataList1_DeleteCommand" OnItemDataBound="DataList1_ItemDataBound"

在代码文件中打

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

Label4.Text = Session["Username"].ToString();

string IDUSE_ = Session["IDUSE"].ToString();

HiddenField1.Value = IDUSE_;

getdata();

}

}

protected void getdata()

{

myco = new MySqlConnection(strconn);

myco.Open();

string IDUSE_ = HiddenField1.Value;

string sql = "select * from logtable where IDUSE= '" + IDUSE_ + "'";

MySqlDataAdapter myAdapter = new MySqlDataAdapter(sql, myco);

DataSet ds = new DataSet();

myAdapter.Fill(ds);

DataList1.DataSource = ds.Tables[0];

DataList1.DataKeyField = "IDRizhi";

DataList1.DataBind();

}

protected void DataList1_ItemDataBound(object source, DataListItemEventArgs e)

{

 ((LinkButton)e.Item.Controls[0].FindControl("LinkButton1")).Attributes.Add("onclick", "return confirm('您确定要删除吗?')");

}

protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)

{

int IDRizhi_ =Int32.Parse( DataList1.DataKeys[e.Item.ItemIndex].ToString());

myco = new MySqlConnection(strconn);

myco.Open();

string sql = "delete  from logtable where IDRizhi=" + IDRizhi_ + "";

mycomm = new MySqlCommand(sql, myco);

dr = mycomm.ExecuteReader();

mycomm.Dispose();

Label6.Text = "已删除";

getdata();

}

 

阅读(511) | 评论(0) | 转发(0) |
0

上一篇:文件共享

下一篇:第二部分日志系统

给主人留下些什么吧!~~