C#提交数据到数据库

时间:2015/11/7 13:38:00来源:互联网 作者:flyso 点击: 1826 次
一,需要App_Code目录下建立一个实体Entity文件夹(App_Code/Entity)
二,在App_Code目录下建立Reg.cs类(App_Code/Reg.cs)
三,在Entity文件夹下建立RegDao.cs类(App_Code/Entity/RegDao.cs)
四,在App_Code目录下建立GetConnectionString类,此类是获取数据库连接字符串(App_Code/GetConnectionString)
五,记得using SqlHelper;
六,一定要从vs.net里面添加引用来导入Sqlhelper.dll文件,否则无用。

Reg.cs(App_Code/Reg.cs)
public class Reg
{
public Reg()
{
  //
// TODO: 在此处添加构造函数逻辑
//
}
private string userId = string.Empty;//此处一地要注意,要小写userId
private string userPassword = string.Empty;
private string email = string.Empty;
private string city = string.Empty;
private string zipCode = string.Empty;
private string userDate = string.Empty;

public string UserId
  {
     get { return this.userId; }
     set { this.userId = value; }
  }

public string UserPassword
{
     get { return this.userPassword; }
     set { this.userPassword = value; }
}

  public string Email
{
   get { return this.email; }
   set { this.email = value; }
}
  public string City
  {
   get { return this.city; }
   set { this.city = value; }
  }

  public string ZipCode
  {
   get { return this.zipCode; }
   set { this.zipCode = value; }
  }

  public string UserDate
{
  get { return this.userDate; }
  set { this.userDate = value; }
  }
}


RegDao.cs(App_Code/Entity/RegDao.cs)
public class RegDao
  {
   public RegDao()
   {
    //
    // TODO: 在此处添加构造函数逻辑
    //
   }
     private static string UserId = "@UserId";

     private static string UserPassword= "@UserPassword";

     private static string Email = "@Email";

     private static string City = "@City";

     private static string ZipCode = "@ZipCode";

     private static string UserDate = "@UserDate";

    //执行SQL语句
     private static string InsertINTO_Userinfo="insert into Userinfo Values(@UserId,@UserPassword,@Email,@City,@ZipCode,@UserDate)";

     private static bool AddUserinfo(Reg user)//这儿的括号里面的表示什么?(对象名 类名)传到AddUserinfo这个私有静态方法。对象名要写对,类名可以任意取。
     {
         SqlParameter[] parms = new SqlParameter[]{
             new SqlParameter(UserId,SqlDbType.Char,50),//以后数据库最好都写成varchar类型,长度给长一些。
             new SqlParameter(UserPassword,SqlDbType.Char,16),
             new SqlParameter(Email,SqlDbType.NVarChar,40),
             new SqlParameter(City,SqlDbType.Char,15),
             new SqlParameter(ZipCode,SqlDbType.Char,10),
             new SqlParameter(UserDate,SqlDbType.DateTime,8)};
    
         parms[0].Value=user.UserId;
         parms[1].Value=user.UserPassword;
         parms[2].Value=user.Email;
         parms[3].Value=user.City;
         parms[4].Value=user.ZipCode;
         parms[5].Value=user.UserDate;
  
         //获取连接字符串,插入数据到SQL数据库
   int tag = SqlHelper.SqlHelpDao.ExecuteNonQuery(GetConnectString.ConnectionString, CommandType.Text, InsertINTO_Userinfo, parms);
         if (tag > 0)
         {
             return true;
         }
         return false;
     }
}


Register.aspx中按钮事件
protected void regBtn_Click(object sender, EventArgs e)
     {
         Reg user = new Reg();
          
         user.UserId = this.UserId.Text.Trim();
         user.UserPassword = this.UserPassword.Text.Trim();
         user.Email = this.Email.Text.Trim();
         user.City = this.City.Text.Trim();
         user.ZipCode = this.Zipcode.Text.Trim();
         //if (RegDao.AddUserinfo(User) == true)
         //{
         //    Response.Write("<script>alert('注册成功')</script>");
         //}
     }


(App_Code/GetConnectionString)
public abstract class GetConnectString
{
public GetConnectString()
{
  //
  // TODO: 在此处添加构造函数逻辑
  //
}
  public static readonly string ConnectionString=ConfigurationManager.ConnectionStrings["WebShareDBConnectionString"].ConnectionString;
}


Copyright © 2005 - 2016 flyso.cn. 飞搜 版权所有 鄂ICP备11002783号-3