1.增加insert into
2.删除:delete from
3.更改 update set
4.查询
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace data1024
{
class Program
{
static void Main(string[] args)
{
#region 如何打开连接
//连接数据库的步骤:
//1.创建连接字符串:含有要连接哪台服务器的那个数据库的实例,用户名是多少,密码是多少
//Data Source:连接哪个服务器的哪个实例
///本机连接:ECUST-GLOBAL\PROFITSUITE 这个是本机的数据库地址
///或者本机ip 127.0.0..1或者localhost
//initial Catalog:idtest:哪个数据库
//Integrated Security:数据库连接验证方式,如果是windows连接:integrated Security=true
//如果是SQL Sever:User ID=sa;Password=123
//Data Source=ECUST-GLOBAL\\PROFITSUITE;
//string str = "Data Source=ECUST-GLOBAL\\PROFITSUITE;INITIAL CATALOG=dbtest;User ID=sa;Password=123";
2.创建链接对象
//using (SqlConnection con=new SqlConnection(str))
//{
// //测试,打开链接
// //3.打开连接(如果打开数据连接没有问题,表示连接成功)
// con.Open();
// //4.关闭连接,释放资源
// con.Close();
// //con.Dispose
//}
//Console.WriteLine("关闭连接,释放资源");
//Console.Read();
#endregion
#region 通过ado.net向表中插入一条数据
1.连接字符串
//string constr = "Data Source=ECUST-GLOBAL\\PROFITSUITE;Initial Catalog=dbtest;Integrated Security=True";
2.创建连接对象
//using (SqlConnection con = new SqlConnection(constr))
//{
// //3.打开连接
// //4.编写SQL语句:Insert
// string sql = "insert into StudentInfo values('FF',1,'2010',3)";
// using (SqlCommand cmd = new SqlCommand(sql, con))
// {
// con.Open();
// int r = cmd.ExecuteNonQuery();
// Console.WriteLine("成功插入了{0}行数据",r);
// }
// Console.WriteLine("OK");
//}
#endregion
#region 删除一条数据
//string str = "Data Source=ECUST-GLOBAL\\PROFITSUITE;INITIAL CATALOG=dbtest;User ID=sa;Password=123";
//using (SqlConnection con=new SqlConnection(str))
//{
// string sql = "delete from StudentInfo where sid=8";
// using (SqlCommand cmd=new SqlCommand(sql,con))
// {
// con.Open();
// int r=cmd.ExecuteNonQuery();
// Console.WriteLine("成功删出{0}条",r);
// }
//}
#endregion
#region 修改语句update
//string str = "Data Source=ECUST-GLOBAL\\PROFITSUITE;Initial Catalog=dbtest;integrated Security=true ";
//using (SqlConnection con=new SqlConnection(str))
//{
// string sql = "update StudentInfo set sName='GG',sGender=1,sBirthday='2010' where sId=4";
// using (SqlCommand cmd=new SqlCommand(sql,con))
// {
// con.Open();
// int r= cmd.ExecuteNonQuery();
// Console.WriteLine("{0}",r);
// }
//}
#endregion
#region 查询select
string str = "Data Source=ECUST-GLOBAL\\PROFITSUITE;Initial Catalog=dbtest;Integrated Security=true";
using (SqlConnection con=new SqlConnection(str))
{
string sql = "select count(*) from StudentInfo";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
object r=(int)cmd.ExecuteScalar();
Console.WriteLine("{0}",r);
}
}
#endregion
}
}
}