Вы находитесь на странице: 1из 7

LOAD AND UPDATE

Fill data in drop down list


SqlConnection cn = new SqlConnection();

cn.ConnectionString = @"Data source=NASARPC\SQLEXPRESS; initial catalog=MCS2;integrated Security=sspi"; SqlCommand cmd = new SqlCommand("select * from Intro1", cn); cn.Open(); SqlDataReader dr = cmd.ExecuteReader(); DropDownList1.DataSource = dr; DropDownList1.DataValueField = "Sno"; DropDownList1.DataBind();

Load data
SqlConnection cn = new SqlConnection(); cn.ConnectionString = @"Data source=NASAR-

PC\SQLEXPRESS; initial catalog=MCS2;integrated Security=sspi"; string str = DropDownList1.SelectedValue; SqlCommand cmd = new SqlCommand("select * from Intro1 where Sno="+str+" ", cn); cn.Open(); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); TextBox1.Text = (string)dr["Name"]; TextBox2.Text = dr[2].ToString(); dr.Close(); cn.Close();

update
SqlConnection cn = new SqlConnection();

cn.ConnectionString = @"Data source=NASARPC\SQLEXPRESS; initial catalog=MCS2;integrated Security=sspi"; SqlCommand cmd = new SqlCommand("update Intro1 set Name=@name, Address=@address where Sno=@Sno", cn); cn.Open();

cmd.Parameters.AddWithValue("@Sno",DropDownList1.Selected Value); cmd.Parameters.AddWithValue("@Name", TextBox1.Text); cmd.Parameters.AddWithValue("@Address", TextBox2.Text); cmd.ExecuteNonQuery(); cn.Close();

Load value using data set


1. create connection 2. Create a DataAdapter object, which represents the link

between the database and your DataSet object. You can specify SQL or another type of command that is used to retrieve data as part of the constructor object of the DataAdapter. This sample uses a SQL statement that retrieves records from the Authors table in the Pubs database.SqlDataAdapter daAuthors = new SqlDataAdapter("Select * From Authors", objConn); 3. You must declare and create an instance of a DataSet object, at which time you can supply a name for the entireDataSet before you can start to load any data. The name may contain several distinct tables.DataSet dsPubs = new DataSet("Pubs");

4. The SqlDataAdapter class provides two

methods, Fill and FillSchema, that are crucial to loading this data. Both of these methods load information into a DataSet. Fill loads the data itself, and FillSchema loads all of the available metadata about a particular table (such as column names, primary keys, and constraints). A good way to handle the data loading is to run FillSchema followed by Fill. For example: daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors"); daAuthors.Fill(dsPubs,"Authors"); If you only use Fill, you can only load the basic metadata that is required to describe the column names and data types. The Fill method does not load primary key information. To change this default behavior, you can set theMissingSchemaAction property of the DataAdapter object to MissingSchemaAction.AddWithKey, which loads the primary key metadata along with the default information. For example:daAuthors.MissingSchemaAction = MissingSchemaAction.AddWithKey; daAuthors.Fill(dsPubs,"Authors");

code
SqlConnection cn = new SqlConnection(); cn.ConnectionString = @"Data source=NASAR-

PC\SQLEXPRESS; initial catalog=MCS2;integrated Security=sspi"; string str = DropDownList1.SelectedValue; SqlDataAdapter da = new SqlDataAdapter("select * from Intro1 where Sno=" + str + " ", cn); DataSet ds = new DataSet("Info1"); da.Fill(ds, "Info1"); if (ds.Tables[0].Rows.Count > 0) { TextBox1.Text=ds.Tables[0].Rows[0][1].ToString(); TextBox2.Text = ds.Tables[0].Rows[0][2].ToString(); }

Вам также может понравиться