C# ADO.N黑桃K平台ET SqlCommand

大发捕鱼 http://www.cnmpsolar 评论

C# ADO.NET SqlCommand - ExecuteScalar The ExecuteScalar() in C# SqlCommand Object is using for retrieve a single value from Database after the execution of the SQL Statement. The ExecuteScalar() executes SQL statements as well as Stored

C# ADO.NET SqlCommand - ExecuteScalar

The ExecuteScalar() in C# SqlCommand Object is using for retrieve a single value from Database after the execution of the SQL Statement. The ExecuteScalar() executes SQL statements as well as Stored Procedure and returned a scalar value on first column of first row in the returned Result Set.

If the Result Set contains more than one columns or rows , it will take only the value of first column of the first row, and all other values will ignore. If the Result Set is empty it will return a NULL reference.

It is very useful to use with aggregate functions like Count(*) or Sum() etc. When compare to ExecuteReader() , ExecuteScalar() uses fewer System resources.


Next :  C# ADO.NET OleDbCommand - ExecuteScalar




Download Source Code

Print Source Code


using System; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; SqlConnection cnn ; SqlCommand cmd ; string sql = null; connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; sql = "Your SQL Statement Here like Select Count(*) from product"; cnn = new SqlConnection(connetionString); try { cnn.Open(); cmd = new SqlCommand(sql, cnn); Int32 count = Convert.ToInt32(cmd.ExecuteScalar()); cmd.Dispose(); cnn.Close(); MessageBox.Show (" No. of Rows " + count); } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }

C# ADO.N黑桃K平台ET SqlCommand

大发捕鱼_黑桃K平台:C# ADO.N黑桃K平台ET SqlCommand

喜欢 (0) or 分享 (0)
发表我的评论
取消评论

表情

您的回复是我们的动力!

  • 昵称 (必填)

网友最新评论