using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace UseSqlCm
{
class Program
{
static void Main(string[] args)
{
string strCon = "Data Source=.;Initial Catalog=Northwind;uid=sa;pwd=Daniel";
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand cmd = con.CreateCommand())
{
//string strSql = "select count(*) from Customers";
//cmd.CommandText = strSql;
//con.Open();
//int intCount = Int32.Parse(cmd.ExecuteScalar().ToString()); //ExecuteScaler();
//Console.WriteLine(intCount);
//cmd.CommandText = "select * from Customers";
//string strS = cmd.ExecuteScalar().ToString(); //ExecuteScalar() 返回结果集的首行首列
//Console.WriteLine(strS); //ALFKI 返回结果集的首行首列
string ins = "insert into Categories(CategoryName) values ('CateNameTest')"; //插入
string upt = "update Categories set CategoryName = 'aa' where CategoryID=9"; //修改
string del = "deleted from Categories where CategoryID=9"; //删除ID=9的记录
con.Open();
cmd.CommandText = ins;
int intResult = cmd.ExecuteNonQuery();
//ExecuteNonQuery执行一个SqlCommand返回操作影响的行数,这个多半是判断操作是否成功的,
//例如插入删除和更新操作,如果影响行数为0的话,则证明操作是不成功的。大于0证明操作成功
if (intResult > 0)
{
Console.WriteLine("成功插入" + intResult + "条记录");
}
else
{
Console.WriteLine("插入失败!");
}
}
}
Console.ReadKey();
}
}
}