类中操作数据
access数据库:
实体类:同上
操作类:personOperater
在2.0中,智能感知没有Add方法,我们可以手工输入,现在暂没有测试AddRange 和AddWithValue的用法,以下通过测试,
sql数据库:
实体类:person
操作类:personOperater
sql数据库:
实体类:person
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for person
/// </summary>
public class person
{
public string pName;
public string pPwd;
public string pEmail;
public person()
{
//
// TODO: Add constructor logic here
//
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for person
/// </summary>
public class person
{
public string pName;
public string pPwd;
public string pEmail;
public person()
{
//
// TODO: Add constructor logic here
//
}
}
操作类:personOperater
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
/// <summary>
/// Summary description for personOperater
/// </summary>
public class personOperater
{
public personOperater()
{
//
// TODO: Add constructor logic here
//
}
public static bool insertPerson(person p)
{
SqlConnection myconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["laura"].ToString());
myconn.Open();
SqlCommand mycmd = new SqlCommand("insert into users(UserName,UserPwd,UserEmail) values(@pName,@pPwd,@pEmail)", myconn);
SqlParameter para = new SqlParameter("@pName", SqlDbType.VarChar, 20);
para.Value = p.pName;
mycmd.Parameters.Add(para);
para = new SqlParameter("@pPwd", SqlDbType.VarChar, 50);
para.Value = p.pPwd;
mycmd.Parameters.Add(para);
para = new SqlParameter("@pEmail", SqlDbType.VarChar, 50);
para.Value = p.pEmail;
mycmd.Parameters.Add(para);
mycmd.ExecuteNonQuery();
myconn.Close();
return true;
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
/// <summary>
/// Summary description for personOperater
/// </summary>
public class personOperater
{
public personOperater()
{
//
// TODO: Add constructor logic here
//
}
public static bool insertPerson(person p)
{
SqlConnection myconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["laura"].ToString());
myconn.Open();
SqlCommand mycmd = new SqlCommand("insert into users(UserName,UserPwd,UserEmail) values(@pName,@pPwd,@pEmail)", myconn);
SqlParameter para = new SqlParameter("@pName", SqlDbType.VarChar, 20);
para.Value = p.pName;
mycmd.Parameters.Add(para);
para = new SqlParameter("@pPwd", SqlDbType.VarChar, 50);
para.Value = p.pPwd;
mycmd.Parameters.Add(para);
para = new SqlParameter("@pEmail", SqlDbType.VarChar, 50);
para.Value = p.pEmail;
mycmd.Parameters.Add(para);
mycmd.ExecuteNonQuery();
myconn.Close();
return true;
}
}
access数据库:
实体类:同上
操作类:personOperater
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
/// <summary>
/// Summary description for personOperater
/// </summary>
public class personOperater
{
public personOperater()
{
//
// TODO: Add constructor logic here
//
}
public static bool insertPerson(person p)
{
OleDbConnection myconn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["lxj"].ToString());
myconn.Open();
OleDbCommand mycmd = new OleDbCommand("insert into users(UserName,UserPwd,UserEmail) values(@pName,@pPwd,@pEmail)", myconn);
OleDbParameter para= new OleDbParameter("@pName",OleDbType.VarChar, 20);
para.Value = p.pName;
mycmd.Parameters.Add(para);
para = new OleDbParameter("@pPwd",OleDbType.VarChar, 50);
para.Value = p.pPwd;
mycmd.Parameters.Add(para);
para = new OleDbParameter("@pEmail", OleDbType.VarChar, 50);
para.Value = p.pEmail;
mycmd.Parameters.Add(para);
mycmd.ExecuteNonQuery();
myconn.Close();
return true;
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
/// <summary>
/// Summary description for personOperater
/// </summary>
public class personOperater
{
public personOperater()
{
//
// TODO: Add constructor logic here
//
}
public static bool insertPerson(person p)
{
OleDbConnection myconn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["lxj"].ToString());
myconn.Open();
OleDbCommand mycmd = new OleDbCommand("insert into users(UserName,UserPwd,UserEmail) values(@pName,@pPwd,@pEmail)", myconn);
OleDbParameter para= new OleDbParameter("@pName",OleDbType.VarChar, 20);
para.Value = p.pName;
mycmd.Parameters.Add(para);
para = new OleDbParameter("@pPwd",OleDbType.VarChar, 50);
para.Value = p.pPwd;
mycmd.Parameters.Add(para);
para = new OleDbParameter("@pEmail", OleDbType.VarChar, 50);
para.Value = p.pEmail;
mycmd.Parameters.Add(para);
mycmd.ExecuteNonQuery();
myconn.Close();
return true;
}
}