using System; using System.Data.SqlClient; namespace ConsoleApplication { class Program { static void Main(string[] args) { SqlConnection con = new SqlConnection("server=.\\sqlexpress;uid=sa;pwd=123456;database=stuDB"); con.Open(); SqlCommand com = con.CreateCommand(); com.CommandText = "select * from stuInfo"; SqlDataReader reader = com.ExecuteReader(); while (reader.Read()) Console.WriteLine(reader["stuName"].ToString() + reader["stuAge"].ToString() + reader["stuSex"].ToString()); reader.Close(); con.Close(); Console.Read(); } } }
本文介绍了一个使用C#连接SQL Server数据库并读取数据的示例代码。通过SqlConnection建立连接,SqlCommand执行SQL语句,SqlDataReader读取结果集,展示了如何将学生信息表中的数据读取并输出到控制台。
732

被折叠的 条评论
为什么被折叠?



