c#dbconnection怎么连数据库
发布网友
发布时间:2022-09-18 05:08
我来回答
共1个回答
热心网友
时间:2023-10-20 11:44
vote
using System.Data.SqlClient;
//
// First access the connection string, which may be autogenerated in Visual Studio for you.
//
string connectionString = "Write your sql connection string"
//
// In a using statement, acquire the SqlConnection as a resource.
//
using (SqlConnection con = new SqlConnection(connectionString))
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code shows how you can use an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// process it
}
}
}