using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace Test1
{
class Program
{
static void Main(string[] args)
{
string str = "Data Source=.;Initial Catalog=MyTest;User ID=sa;password=688886";
/-------------------------------------------------
SqlConnection为建立和数据库连接的对象
//using (SqlConnection conn = new SqlConnection(str))
//{
// conn.Open(); //打开连接
// conn.BeginTransaction();
// //通过连接创建一个向数据库发命令(Command)的对象SqlCommand对象。
// using (SqlCommand cmd = conn.CreateCommand())
// { //CommandText为要执行的SQL语句
// cmd.CommandText = "insert into T_Student(Name,Age) values('张雷',20000)";
// cmd.ExecuteNonQuery();//执行,ExecuteNoQuery一般用来执行Update,Delete,Insert语句
// }
//}
/-------------------------------------------------
//using (SqlConnection conn = new SqlConnection(str))
//{
// conn.Open();
// using (SqlCommand cmd = conn.CreateCommand())
// {
// cmd.CommandText = "select count(*) from T_Student where age < 200";
// //ExecuteScalar一般用来执行有且只有一行一列返回值的SQL语句。
// object ob = cmd.ExecuteScalar();
// int count = (int)ob;
// Console.WriteLine(count+"数据!");
// }
//}
/-------------------------------------------------
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//获得自动增长字段的值
cmd.CommandText = "insert into person(name) values('AAA');";
cmd.ExecuteNonQuery();
}
}
Console.WriteLine("execute success!");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace Test1
{
class Program
{
static void Main(string[] args)
{
string str = "Data Source=.;Initial Catalog=MyTest;User ID=sa;password=688886";
/-------------------------------------------------
SqlConnection为建立和数据库连接的对象
//using (SqlConnection conn = new SqlConnection(str))
//{
// conn.Open(); //打开连接
// conn.BeginTransaction();
// //通过连接创建一个向数据库发命令(Command)的对象SqlCommand对象。
// using (SqlCommand cmd = conn.CreateCommand())
// { //CommandText为要执行的SQL语句
// cmd.CommandText = "insert into T_Student(Name,Age) values('张雷',20000)";
// cmd.ExecuteNonQuery();//执行,ExecuteNoQuery一般用来执行Update,Delete,Insert语句
// }
//}
/-------------------------------------------------
//using (SqlConnection conn = new SqlConnection(str))
//{
// conn.Open();
// using (SqlCommand cmd = conn.CreateCommand())
// {
// cmd.CommandText = "select count(*) from T_Student where age < 200";
// //ExecuteScalar一般用来执行有且只有一行一列返回值的SQL语句。
// object ob = cmd.ExecuteScalar();
// int count = (int)ob;
// Console.WriteLine(count+"数据!");
// }
//}
/-------------------------------------------------
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//获得自动增长字段的值
cmd.CommandText = "insert into person(name) values('AAA');";
cmd.ExecuteNonQuery();
}
}
Console.WriteLine("execute success!");
}
}
}