Connecttion 对象,command 对象
connecttion 分为 SQL.NET 和 OLEDB .NET 数据提供程序,
前者主要连接SQL server 数据库,后者用来连接除SQLSERVER 以外的数据库。
sql.net 数据提供程: sqlconn=new SqlConnection("server=;database=tatabase;uid=user;pwd=password")
OLEDB .NET 数据提供程序:
adocon=new oledbconnection("provider=数据库提供者类型;datasource=数据库服务器地址;inotial="使用那个数据库";user id=用户名;pwd=密码")
数据插入操作:
打开数据连接后:
插入命令:
SqlCommand cmd=new SqlCommand("insert into person values(@pid,@pname,@psex)",con);
分别插入各个元素:
SqlParameter para = new SqlParameter("@pid", SqlDbType.VarChar, 10);
para.Value = p.pid;
cmd.Parameters.Add(para);
para = new SqlParameter("@pname", SqlDbType.VarChar, 20);
para.Value = p.pname;
cmd.Parameters.Add(para);
para = new SqlParameter("@psex", SqlDbType.VarChar, 2);
para.Value = p.psex;
cmd.Parameters.Add(para);
执行:
cmd.ExecuteNonQuery();