[My ASP.NET Learning History] Database Settion (7)

xiaoxiao2021-03-06  57

Using system;

Using system.collections;

Using system.componentmodel;

Using system.data;

Using system.drawing;

Using system.Web;

Using system.Web.SessionState;

Using system.Web.ui;

Using system.Web.ui.webcontrols;

Using system.Web.ui.htmlcontrols;

Using system.data.sqlclient;

Namespace _172._24._17._134

{

///

/// WebForm1 summary description.

///

Public class Webform1: System.Web.ui.page

{

Protected system.web.ui.webcontrols.dataGrid DataGrid1;

Private Void Page_Load (Object Sender, System.EventArgs E)

{

/ / Place the user code here to initialize the page

IF (! page.ispostback)

{

Binddata ();

}

}

#Region Web Form Designer Generated Code

Override protected void oninit (Eventargs E)

{

//

// Codegen: This call is necessary for the ASP.NET Web Form Designer.

//

InitializationComponent ();

Base.onit (E);

}

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.DataGrid1.cancelcommand = new system.Web.ui.WebControls.DataGridCommandeventHandler (this.DataGrid1_cancelcommand);

This.DataGrid1.editcommand = new system.web.ui.webcontrols.dataGridCommandeventHandler (this.DataGrid1_editCommand);

This.DataGrid1.updateCommand = new system.web.ui.webcontrols.dataGridCommandeventHandler (this.DataGrid1_updateCommand);

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

Private void binddata ()

{

String strsql = "SELECT ID, Username, Password, Content, Email, HomePage from Table_1";

String connectionString = "server = (local); user ID = user; pwd = useruser; database = testdb; connection timeout = 5;

SqlConnection Conn = New SqlConnection (Connectionstring);

Sqlcommand cmd = new sqlcommand (strsql, conn); cmd.connection.open ();

// DataSet DS = New Dataset ();

// SqldataAdapter Da = new SqlDataAdapter ();

//

// da.selectcommand = cmd;

//

// da.fill (DS, "Table");

//

// DataGrid1.datasource = ds.tables ["Table"]. Defaultview;

// DataGrid1.databind ();

//

// ds.clear ();

// da.dispose ();

SqlDataReader DR = cmd.executeRead ();

DataGrid1.datasource = DR;

DataGrid1.databind ();

Dr.close ();

cmd.connection.close ();

cmd.connection.dispose ();

}

Public void DataGrid1_EDitcommand (Object Source, System.Web.ui.WebControls.DataGridCommandEventArgs E)

{

DataGrid1.editItemIndex = Convert.Toint32 (E.Item.ItemIndex);

Binddata ();

}

Public void DataGrid1_cancelcommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E)

{

DataGrid1.edititemindex = -1;

Binddata ();

}

Public void DataGrid1_UpdateCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E)

{

String connectionString = "server = (local); user ID = sa; pwd = zxbbugu; database = testdb; connection timeout = 5;

String id = E.Item.cells [0] .Text.toString ();

String username = ((TextBox) E.Item.cells [1] .controls [0]). Text.toString ();

String password = ((TextBox) E.Item.cells [2] .controls [0]). Text.toString ();

String content = ((textBox) E.Item.cells [3] .controls [0]). TEXT.TOSTRING ();

String email = ((textbox) E.Item.cells [4] .controls [0]). TEXT.TOSTRING ();

// String homepage = ((TextBox) E.Item.cells [5] .controls [0]). Text.toString ();

String strsql = "Update Table_1 Set UserName = '" UserName "', Password = '" Password;

// strsql = ", content = '" content ", email ='" email ", homepage = '" homepage; strsql = ", content ='" content "' Email = '" email;

STRSQL = "'where id =" id;

//

// String UserName = (TextBox) E.Item.cells [1] .controls [0]).

// String strsql = @ "update table_1 set username = '" username "' where id =" id;

SqlConnection Conn = New SqlConnection (Connectionstring);

Sqlcommand cmd = new SQLCOMMAND (strsql, conn);

cmd.connection.open ();

cmd.executenonquery ();

cmd.dispose ();

CONN.CLOSE ();

Conn.dispose ();

DataGrid1.edititemindex = -1;

Binddata ();

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-113629.html

New Post(0)