This control is really powerful, although I only know how to use it now!
However, he was really hard to learn, because it took me a lot of time and I never knew how to start it. I finally watched it on MSDN last night, so I finally understood the most superficial usage. Here I come to a conclusion!
This is not easy to get started with. Once you know how to use it, it is easy to go further! So I made a video of what I learned. I hope more friends who are still wandering outside the door will sit down in the room early. After all, it's so hot now !!
Video and all code is
Http://spacenet.supericp.com/thc/ObjectDataSource.rar
Let's not talk about it. Start work !!!
I started to do this because I stayed up late last night, so I was not very skilled !! Once done first!
The following is a code snippet. If you haven't watched the video, you can directly view the snippet here.
Returns a data table string connstring = ConfigurationManager. ConnectionStrings ["two"]. ConnectionString;
// You do not need to know the above line. It is to take out the database connection string and use it later.
Public DataTable dt ()
{// This data control can only return those classes, such as sqldataadapter, dataset, and able. What else have you forgotten? Here we return a data table to the front.
SqlConnection con = new SqlConnection (connstring );
SqlDataAdapter sda = new SqlDataAdapter ();
Sda. selectCommand = new SqlCommand ("SELECT * FROM [Categories] order by [CategoryID] DESC", con); // here we feel that it is difficult to write SQL statements, so I think of a good way, very simple
DataSet ds = new DataSet ();
Sda. Fill (ds, "thc ");
Return ds. Tables ["thc"];
// At This Point, this query is complete, simple !!!
}
Returns a SqlDataReader object public SqlDataReader sdr (int CategoryID)
{// A SqlDataReader, which does not support pagination and sorting by GridView !!
SqlConnection con = new SqlConnection (constring );
SqlCommand cmd = new SqlCommand ("SELECT * FROM [Categories] where [CategoryID] = @ CategoryID order by [CategoryID] DESC", con );
Cmd. Parameters. AddWithValue ("@ CategoryID", CategoryID );
Con. Open ();
SqlDataReader sdr = cmd. ExecuteReader (CommandBehavior. CloseConnection );
Return sdr;
}
// Update public void update (string CategoryName, string Description, int CategoryID)
{// Update
SqlConnection con = new SqlConnection (constring );
SqlCommand cmd = new SqlCommand ("UPDATE [Categories] SET [CategoryName] = @ CategoryName, [Description] = @ Description WHERE [CategoryID] = @ CategoryID", con );
Con. Open ();
Cmd. Parameters. AddWithValue ("@ CategoryName", CategoryName );
Cmd. Parameters. AddWithValue ("@ Description", Description );
Cmd. Parameters. AddWithValue ("@ CategoryID", CategoryID );
Cmd. ExecuteNonQuery ();
}
// Delete the public void delete (int CategoryID) Action)
{
SqlConnection con = new SqlConnection (constring );
SqlCommand cmd = new SqlCommand ("delete from [Categories] WHERE [CategoryID] = @ CategoryID", con );
Con. Open ();
Cmd. Parameters. AddWithValue ("@ CategoryID", CategoryID );
Cmd. ExecuteNonQuery ();
}
Code called by the front-end <asp: ObjectDataSource ID = "objectperformance1" runat = "server" SelectMethod = "dt"
TypeName = "objdb" UpdateMethod = "update" DeleteMethod = "delete">
<UpdateParameters>
<Asp: Parameter Name = "CategoryName" Type = "String"/>
<Asp: Parameter Name = "Description" Type = "String"/>
<Asp: Parameter Name = "CategoryID" Type = "Int32"/>
</UpdateParameters>
<DeleteParameters>
<Asp: Parameter Name = "CategoryID" Type = "Int32"/>
</DeleteParameters>
</Asp: ObjectDataSource>
<Br/>
<Asp: GridView ID = "GridView1" runat = "server" AllowPaging = "True" AllowSorting = "True"
Performanceid = "objectperformance1" DataKeyNames = "CategoryID">
<Columns>
<Asp: CommandField ShowEditButton = "True" ShowDeleteButton = "True"/>
</Columns>
</Asp: GridView>