C# ADO.NE T SqlDataReader

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

C# ADO.NET SqlDataReader SqlDataReader Object provides a connection oriented data access to the SQL Server data Sources from C# applications. ExecuteReader() in the SqlCommand Object sends the SQL statements to the SqlConnection Object a

C# ADO.NET SqlDataReader

SqlDataReader Object provides a connection oriented data access to the SQL Server data Sources from C# applications. ExecuteReader() in the SqlCommand Object sends the SQL statements to the SqlConnection Object and populate a SqlDataReader Object based on the SQL statement or Stored Procedures.

When the ExecuteReader method in the SqlCommand Object execute , it will instantiate a SqlClient.SqlDataReader Object. When we started to read from a DataReader it should always be open and positioned prior to the first record. The Read() method in the DataReader is used to read the rows from DataReader and it always moves forward to a new valid row, if any row exist .


Next :  C# ADO.NET OleDbDataReader




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 sqlCnn ; SqlCommand sqlCmd ; string sql = null; connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; sql = "Your SQL Statement Here , like Select * from product"; sqlCnn = new SqlConnection(connetionString); try { sqlCnn.Open(); sqlCmd = new SqlCommand(sql, sqlCnn); SqlDataReader sqlReader = sqlCmd.ExecuteReader(); while (sqlReader.Read()) { MessageBox.Show(sqlReader.GetValue(0) + " - " + sqlReader.GetValue(1) + " - " + sqlReader.GetValue(2)); } sqlReader.Close(); sqlCmd.Dispose(); sqlCnn.Close(); } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }

C# ADO.NE T SqlDataReader

大发捕鱼_黑桃K平台:C# ADO.NE T SqlDataReader

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

表情

您的回复是我们的动力!

  • 昵称 (必填)

网友最新评论