新增
insert into 表名(列名)values (值)
一次新增多行
insert into 表名(列名)
select 值 union
select 值
删除
delete from 表名 where 删除条件
修改
update 表名 set 列=更新值 where 更新条件
查询
select * from 表名 where 查询条件
天龙八部
//第一步: 定义连接字符串的口令
// Data Source 服务器地址 . 代表本地
//Initial Catalog 数据库名称
//User ID 登录名
// Pwd 密码
String connStr = “data source=.; initial Catalog=T55Info;user id=sa;pwd=sasa”;
//第二步: (需要打using System.Data.SqlClient;包)创建数据库连接对象
//创建连接对象
SqlConnection conn = new SqlConnection(connStr);
try
{
//第三步:打开数据库
//开门 使用媒婆给的口令进行开门
conn.Open();
//第四步: 整理SQL语句
string SQL = string.Format(@"select COUNT (0) from Student
where StudentName=’{0}’ and LoginPwd=’{1}’",name,pwd);
//第五步:获取到command对象
//SqlCommand 对象名=new SqlCommand(SQL语句,连接对象);
SqlCommand comm = new SqlCommand(SQL,conn);
//第六步: 执行并返回值
//comm.ExecuteScalar() 返回一个object类型的值 (需要进行转换!!!)
//扩展:comm.ExecuteScalar() 返回单个值。 (object类型)
//int ExecuteNonQuery() (增,删,改) 返回受影响的行数。(int 类型)
int count=(int)comm.ExecuteScalar();
//第七步,处理返回的结果(此处省略)
}
catch (Exception ex)
{
Console.WriteLine("出现异常: " + ex.Message);
//throw;
}
finally
{
//需要打using System.Data;包。
//Broken 中断
//ConnectionState 表示与数据源的连接状态
//只有在打开(open)和中断 (Broken) 的状态下才需要关闭数据库!
if (ConnectionState.Brokenconn.State || ConnectionState.Openconn.State)
{
//第八步:关闭数据库
conn.Close();
}
}