SqlDataReader rdr = null; SqlConnection con = null; SqlCommand cmd = null; try { // Open connection to the database string ConnectionString = "server=xeon;uid=sa;"+ "pwd=manager; database=northwind"; con = new SqlConnection(ConnectionString); con.Open(); // Set up a command with the given query and associate // this with the current connection. string CommandText = "SELECT Value" + " FROM Table" + " WHERE (Value = 20)"; cmd = new SqlCommand(CommandText); cmd.Connection = con; // Execute the query rdr = cmd.ExecuteReader(); while(rdr.Read()) { label1.text = (rdr["Value"].ToString(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { // Close data reader object and database connection if (rdr != null) rdr.Close(); if (con.State == ConnectionState.Open) con.Close(); }
Thanks Karthik www.f5Debug.net
String strConn= "Data Source=test_server;Initial Catalog=test_Db;User ID=sa;Password=test;"; String strSQLQuery = "select Id, Name,Salary from Employee where Salary =20" SqlDataAdapter daConn= new SqlDataAdapter(strSQLQuery , strConn); DataSet dsData = new DataSet(); // No need to open or close the connection // since the SqlDataAdapter will do this automatically. daConn.Fill(dsData); label1.Text = dsData.Tables[0].Row[0]["Salary"].ToString(); // If output have one record otherwise refer the following loop... foreach (DataRow drRow in dsData.Tables[0].Rows) { lable1.Text += drRow[2].ToString(); }
Thanks, T.Saravanan
Login to post response