usingSystem;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespacecommandthreeproperties{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } SqlConnection Conn; Private voidForm1_Load (Objectsender, EventArgs e) {Conn=NewSqlConnection ("server=.; Database=db_12;uid=sa;pwd=sa"); Conn. Open (); } Private voidButton1_Click (Objectsender, EventArgs e) { Try { if(Conn. State = = ConnectionState.Open | | TextBox1.Text! ="") {SqlCommand cmd=NewSqlCommand (); Cmd. Connection=Conn; Cmd.commandtext="Select COUNT (*) from"+TextBox1.Text.Trim (); Cmd.commandtype=CommandType.Text; inti = Convert.ToInt32 (cmd). ExecuteScalar ());//returns the first column of the first row of the query resultsLabel2. Text ="The data sheets are:"+ i.tostring () +"Bar Data"; } } Catch(Exception ex) {MessageBox.Show (ex). Message); } } }}
C # ExecuteScalar Usage