日期:2014-05-17 浏览次数:20720 次
//实体类
public class E_Users
{
public Guid ID { get; set; }
public string UserID { get; set; }
public string Password { get; set; }
public string Name { get; set; }
}
//界面
public partial class UsersAdd : System.Web.UI.Page
{
protected void btn_Click(object sender, EventArgs e)
{
E_Users usersTable = BLL_Users.getUserByID(mid);
usersTable.ID = GUID.NEW();
usersTable.Name = this.txtName.Text;
usersTable.Password = this.txtConfirmPassword.Text;
usersTable.UserID = this.txtUserID.Text;
//下面这种极容易出错...不用工具,有什么方法写不容易出错呢?
string strSQL = string.Format("INSERT INTO dbo.Users( ID, UserID, [Password], Name ) VALUES ( '{0}','{1}','{2}','{4}')",
usersTable .ID, usersTable .UserID, usersTable .Password, usersTable .Name);
//然后把这个字符串传给sqlcommand提交给数据库,这种笨方法极
//容易出现索引超出与指定的列的数目匹配错误。
}
using (NorthwindEntities entity = new NorthwindEntities())
{
Orders order = new Orders();
order.OrderID = 1001;
//添加其他数据
entity.Orders.AddObject(order);
entity.SaveChanges();//提交到数据库
}
string str = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
using (SqlConnection conn = new SqlConnection(str))
{<