asp.net| data
Using System;
Using System.Data;
Using System.Data.SqlClient;
......
Namespace Xxxx.xxxxxxx
{
<summary>
A summary description of XXXXX.
</summary>
public class Xxxxx:System.Web.UI.Page
{
protected System.Data.DataRow Dr;
protected System.Data.SqlClient.SqlConnection Conn;
protected System.Data.SqlClient.SqlCommand mycmd,mysqlcmd;
private void Page_Load (object sender, System.EventArgs e)
{
String connstr= "Data source=xxx.xxx.xxx.xxx;uid=xxxx;pwd=****;d atabase=xxxx";;
This.conn=new System.Data.SqlClient.SqlConnection (CONNSTR);
}
Binding Data Using SqlDataReader
private void Xxxx_bound ()
{
Conn. Open ();
Sql= "SELECT * from XXXX";
MYCMD = conn. CreateCommand ();
Mycmd.commandtext =sql;
SqlDataReader Sqr=mycmd.executereader ();
while (SQR. Read ())
{
Tb_xxxx. Text=sqr[0]. ToString (). Trim ();
......
for (int i=0; i<ddl_xxxx. Items.Count; i++)
{
if (ddl_xxxx. Items[i]. Value.trim () = = Sqr[8]. ToString (). Trim ())
{
Ddl_xxxx. SelectedIndex = i;
}
}
&NBSP;&NBSP;&NBSP;&NBSP;
}
conn. Close ();
}
//Binding after data is accessed using DataTable
private void Datagrid2_bound ()
{
Conn. Open ();
sqlcommand mycommand= Conn. CreateCommand ();
mycommand.commandtext= "sp_xxxxxx";
myCommand.CommandType=CommandType.StoredProcedure;
sqlparameter para_type = MyCommand.Parameters.Add ("@sql", SqlDbType.VarChar);
para_type.value = "";
sqldatareader sqldr1 = Mycommand.executereader ();
datatable dt=new DataTable ();
dt. Columns.Add (New DataColumn ("XXXX", typeof (String));
.....
while (SQLDR1. Read ())
{
dr = dt. NewRow ();
for (int i=0; i<sqldr1. FieldCount; i++)
{
dr[i] = sqldr1[ I]. ToString ();
}
dt. Rows.Add (DR);
}
DataView Source = new DataView (DT);
Datagrid2.datasource=source;
L_count. text= "Total" +source.count.tostring () + "record";
Datagrid2.databind ();
Conn. Close ();
}
Binding Data using a dataset
public void Bindgrid (String SortField)
{
String SqlString = "Select User name, name from Ryxx";
Conn. Open ();
SqlDataAdapter mycommand = new SqlDataAdapter (SqlString, Conn);
DataSet ds = new DataSet ();
Mycommand.fill (ds, "Ryxx");
DataView Source = ds. tables["Ryxx"]. DefaultView;
Source.sort = SortField;
Datagrid1.datasource=source;
Datagrid1.databind ();
Conn. Close ();
}
Execute an SQL statement other than select
private void Ddl_xxxx_bind ()
{
String Updateshry;
updateshry= "Update ryxx set user name = ' Fdsas ', name = ' KJDSJ '";
SqlCommand comshry=new SqlCommand (updateshry,conn);
Conn. Open ();
Try
{
Cm.executenonquery ();
}
catch (SqlException E)
{
This. Response.Write ("<script language=javascript>alert (' Anomaly Information:" +e.tostring () + "');</script>");
}
Conn. Close ();
}
}
}