效果
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
//连接字符串
string connStr = "Data Source=.;Initial Catalog=dbtwo;Integrated Security=True";
//获得连接对象
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
//需要一个执法者
//去哪里,conn
//干什么,sql语句
string sql = "select name from Table_3";
SqlCommand cmd = new SqlCommand(sql,conn);
Console.WriteLine(cmd);
//开放连接
//让执法者干活
//对象.方法名()
Console.WriteLine(cmd.ExecuteScalar());
//暂停
Console.ReadKey();
}
}
}