Models / customer.cs //放資料庫的資料
----------------------------------
public class customer
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}
-----------------------------------
---------------------------------------------------------------------------------------------------------
Repository / CustomerRepository.cs // 主要程式區
------------------------------------
public void Create(customer Model) //新增
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test1113_DataSource"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT customer (Id,Username,Password,Email) " +
"VALUES (@Id,@Username,@Password,@Email)";
cmd.Parameters.AddWithValue("@Id", Model.Id);
cmd.Parameters.AddWithValue("@Username", Model.Username);
cmd.Parameters.AddWithValue("@Password", Model.Password);
cmd.Parameters.AddWithValue("@Email", Model.Email);
if (cmd.ExecuteNonQuery() == 0)
{
// handling data errors.
}
con.Close();
}
public int GetLatestIdentity() //抓最後一個id並+1存回去
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test1113_DataSource"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT ISNULL(MAX(Id), 0) + 1 FROM customer";
int id = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
return id;
}
------------------------------------
---------------------------------------------------------------------------------------------------------
Repository / InterfaceCustomer.cs //介面用
------------------------------------
interface InterfaceCustomer<CModel>
{
void Create(CModel Model);
int GetLatestIdentity();
}
------------------------------------
沒有留言:
張貼留言