Sqldatareader的用法:就是读数据库的读取操作
实例代码:
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())//读取下一条记录
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));//读取的是的是哪一列;
}
// Call Close when done reading.
reader.Close();
}
}
关注点:对于sqldatareader中的读取操作要进行关闭;没读一次reader就读取下一条记录:
asp.net中读取配置文件中数据库的连接字符串
第一要引入头文件system.configuration文件
第二读取字符串的方法:
string connectionstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();