接著來講講資料庫連線的部分....
Mysql
類別Class
public class Category
{
public int CateType { get; set; }
public string CategoryID { get; set; }
public string CategoryName { get; set; }
}
連線字串
ConnString = Server=127.0.0.1;userid=root; password=11111111; database=test
連線 Insert or Update or Delete
using (var conn = new MySqlConnection(ConnString)){
conn.Open();
using (var command = conn.CreateCommand()){
command.CommandText = "Insert Into Category (CateType,Category,Category_Name,ModifyDate,ModifyEID) VALUES(@CateType,@Category,@Category_Name,Now(),@InputUser)";
command.Parameters.AddWithValue("@CateType", 0);
command.Parameters.AddWithValue("@Category", "0001");
command.Parameters.AddWithValue("@Category_Name", "AA");
command.Parameters.AddWithValue("@InputUser", "111");
command.ExecuteNonQuery();
}
}
連線 Select
var result = new Category();
using (var conn = new MySqlConnection(ConnString)){
conn.Open();
using (var command = conn.CreateCommand()){
command.CommandText = "SELECT Category, Category_Name FROM Category WHERE Category = @Category And CateType = @CateType";
command.Parameters.AddWithValue("@Category", "0001");
command.Parameters.AddWithValue("@CateType", 0);
using (var reader = command.ExecuteReader()){
if (reader.HasRows){
while (reader.Read()){
result = new Category(){
CategoryID = (string)reader["Category"],
CategoryName = (reader.IsDBNull(reader.GetOrdinal("Category_Name"))) ? "" : (string)reader["Category_Name"]};
}
return result;
}
else{
return result;
}
}
}
}
註解:如果懂SQL語法的人走ADO.NET 是一個很好的處理方式,在後面會講Entity Framework處理方式