我也是刚学的,希望这些东西对你有用
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace AdoDotNetDemo
{
class Program
{
static void Main(string[] args)
{
QueryDemo();
//DataUpdateDemo();
}
/// <summary>
/// 演示使用执行数据的插入、更新、删除
/// </summary>
private static void DataUpdateDemo()
{
string strConnectionString = @"Data Source=(local)\SQLExpress;Initial Catalog=demo_1;Integrated Security=SSPI;";
//创建并打开数据库联接
SqlConnection conn = new SqlConnection(strConnectionString);
conn.Open();
//利用SqlCommand对象执行insert语句插入数据
string strInsert = "insert into TB_STUDENTS (STU_KEY,STU_ID,STU_NAME,STU_GENDER,STU_BIRTHDATE,CLS_ID) values ("
+ "'c805909e-3aac-43e3-9cef-d205c494015c','20090917','李隆基','男','1989-02-17','1d5de51e-0720-49fa-9059-c48eca68b4dd')";
SqlCommand cmd = new SqlCommand(strInsert, conn);
//可以在SqlCommand的构造方法里面传入SQL语句作为参数
int insertedCount = cmd.ExecuteNonQuery();
//调用ExecutedNonQuery方法执行insert/update/delete语句完成数据的插入/修改/删除
Console.WriteLine("{0}行数据被插入。", insertedCount);
Console.ReadLine();
//利用SqlCommand对象执行update语句更新数据表中的数据
string strUpdate = "update TB_STUDENTS set STU_NAME='朱棣' where STU_KEY='d0c5130a-05c2-435f-9ba3-a35463a7d98a'";
= strUpdate;
//修改cmd对象的SQL语句,由刚才的insert变成了update,可以通过CommandText属性为SqlCommand对象指定要执行的SQL语句
int updatedCount = cmd.ExecuteNonQuery();
Console.WriteLine("{0}行数据被修改。", updatedCount);
Console.ReadLine();
//利用SqlCommand对象执行delete语句删除记录
string strDelete = "delete from TB_STUDENTS where STU_KEY='c805909e-3aac-43e3-9cef-d205c494015c'";
= strDelete;
int deletedCount = cmd.ExecuteNonQuery();
Console.WriteLine("{0}行数据被删除。", deletedCount);
conn.Close();
Console.ReadLine();
}
/// <summary>
/// 演示使用执行select语句进行数据查询
/// </summary>
private static void QueryDemo()
{
//创建数据库联接字符串
//Data Source=(local)\SQLExpress;
用来指定要访问的SQLServer服务实例,这里是本机的SQLExpress实例
//Initial Catalog=demo_1;
指定要访问的数据库,这里的数据库是demo_1;
//Integrated Security=SSPI; 指定访问SQLServer时使用Windows集成身份验证;
//如果想使用SQLServer身份验证,可以用 uid=用户名;pwd=口令;指定用户名和口令
string strConnectionString = @"Data Source=(local)\SQLExpress;Initial Catalog=demo_1;Integrated Security=SSPI;";
//创建并打开数据库联接
SqlConnection conn = new SqlConnection(strConnectionString);
conn.Open();
//利用SqlCommand对象执行Select语句
string strSelect = "select * from TB_STUDENTS";
SqlCommand cmd = new SqlCommand(strSelect, conn);
SqlDataReader dataReader = cmd.ExecuteReader();
//dataReader就是Select语句返回的结果集
//循环取出dataReader中的每一条记录,如果Read方法返回true,说明成功指向下一条记录,否则
//表示已经到了结果集尾部,结束循环
while (dataReader.Read())
{
Guid stuKey = (Guid)dataReader["STU_KEY"];
//取出STU_KEY字段的值,SQLServer中的uniqueidentifier类型对应于C#中的Guid类型
string stuId = (string)dataReader["STU_ID"];
//SQLServer中的varchar类型对应于C#中的string类型
string stuName = (string)dataReader["STU_NAME"];
//SQLServer中的nvarchar类型对应于C#中的string类型
string stuGender = (string)dataReader["STU_GENDER"];
DateTime stuBirthDate = (DateTime)dataReader["STU_BIRTHDATE"];
//datetime -> DateTime
Guid clsId = (Guid)dataReader["CLS_ID"];
Console.WriteLine("学生代码{0},学号{1},姓名{2},性别{3},出生日期{4},班级代码{5}",
stuKey, stuId, stuName, stuGender, stuBirthDate, clsId);
}
dataReader.Close();
//关闭dataReader,释放资源
conn.Close();
//关闭Connection,释放资源
}
}
}