aspx,,,,,新闻条目,,,,,,,
``csharp,using System.Data;,using System.Data.SqlClient;,
`,,2. 创建数据库连接字符串:,
`csharp,string connectionString = "Server=服务器地址;Database=数据库名;User Id=用户名;Password=密码;";,
`,,3. 创建SqlConnection对象并打开连接:,
`csharp,using (SqlConnection connection = new SqlConnection(connectionString)),{, connection.Open();,},
`,,4. 创建SqlCommand对象并执行查询:,
`csharp,using (SqlCommand command = new SqlCommand("SELECT * FROM YourTableName", connection)),{, using (SqlDataReader reader = command.ExecuteReader()), {, while (reader.Read()), {, // 处理查询结果, }, },},
`,,5. 关闭连接(using语句会自动关闭):,
`csharp,connection.Close();,
``,,以上代码示例展示了如何在ASP.NET中使用ADO.NET从SQL数据库中读取数据。Powered By Z-BlogPHP 1.7.3
Copyright Your WebSite.Some Rights Reserved.