2011年11月30日

Android 讀取資料庫(SQLite)的下拉式選單(Spinner)

DbHelper.java
 
public class DbHelper extends SQLiteOpenHelper {
 private static final String DATABASE_NAME = "xxxx.sqlite"; //資料庫名稱
 private static final int DATABASE_VERSION = 1; //資料庫版本
 private static final String table_name = "table";
 private SQLiteDatabase db;
 
 public DbHelper(Context context) { //建構子
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
  db = this.getWritableDatabase();
 }
 
 @Override
 public void onCreate(SQLiteDatabase db) { //初始化資料庫 
  DATABASE_CREATE_TABLE =
       "create table class1 ("
           + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
           + "class_name TEXT"
       + ");";
  db.execSQL(DATABASE_CREATE_TABLE);
  String TABLE_Insert = 
    "INSERT INTO class1 (class_name) VALUES ('食');";
  db.execSQL(TABLE_Insert);
  TABLE_Insert = 
    "INSERT INTO class1 (class_name) VALUES ('衣');";

  db.execSQL(TABLE_Insert);
 }

public Cursor getclass1(){  //取得class1資料表內容
  return db.query("class1",new String[] {"_id","class_name"} , null, null, null, null, null);
 }

MainActivity.java

public class MainActivity extends Activity {
 private DbHelper dbHelper; 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstlayout);
        Spinner sp01 = (Spinner)findViewById(R.id.spinner01);
        dbHelper = new DbHelper(this);
        final Cursor cursor = dbHelper.getclass1();
         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item ,cursor, new String[] { "class_name" }, new int[] {android.R.id.text1});
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         sp01.setAdapter(adapter);

          //.....處理點選後行為....

 }

}

2011年11月29日

ASP.NET C# 用Gmail SMTP寄信

利用C# Mailmessage方法

string smtpserver = "smtp.gmail.com"; //Gmail smtp 主機
string smtpaccount = "xxx@gmail.com"; //Gmail 帳號
string smtppassword = "password";     //Gmail 密碼
            
MailMessage msg = new MailMessage();
msg.From = new MailAddress("xxx@gmail.com", "老王");
msg.To.Add("ooo@yahoo.com");  //收件人
msg.Bcc.Add("xxx@yahoo.com"); //密件副本 
msg.Subject = "標題";     
msg.IsBodyHtml = true;       //啟用Html格式內文
msg.Body = "信件內容";
SmtpClient smtp = new SmtpClient(smtpserver);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Port = 587;         //Gmail smtp port
smtp.EnableSsl = true;   //開啟ssl模式
smtp.Credentials = new System.Net.NetworkCredential(smtpaccount, smtppassword);
smtp.Send(msg); 

2011年11月28日

ASP.NET LINQ To Entity Model 新增/刪除/更新 語法範例

Database TestDB如下:
新增 (Insert):

using (TestDBEntities ctx = new TestDBEntities())

{

//Create new Department

Dept d = new Dept() { DeptName = "ADO Entity" };

//Create new Employee 1

EmpDept ed1 = new EmpDept() { EmpName = "ADO Employee 1" };

//Create new Employee 2

EmpDept ed2 = new EmpDept() { EmpName = "ADO Employee 2" };

//Add employee to the Dept *OBJECT*

d.EmpDept.Add(ed1);

d.EmpDept.Add(ed2);

//Updating the context

ctx.AddToDept(d);

//Save to Database

ctx.SaveChanges();

}

更新(Update):

using (TestDBEntities ctx = new TestDBEntities())

{

//Get an existing Department

Dept dep = (from d in ctx.Dept

where d.DeptId == 22

select d).First();

//Set new Department name

dep.DeptName = "ADO.NET 3.0";

//Create new Employee 2

EmpDept ed2 = new EmpDept() { EmpName = "ADO 2" };

//Add *new* employee to the Dept *OBJECT*

dep.EmpDept.Add(ed2);

//Save to Database

ctx.SaveChanges();

}  

刪除(Delete):

using (TestDBEntities ctx = new TestDBEntities())

{

//Get an existing Department

Dept dep = (from d in ctx.Dept.Include("EmpDept")

where d.DeptId == 22

select d).First();

/*

Needd to do ToList() becuase once you delete

a record then iteration will not be possible.

*/

foreach (EmpDept ed in dep.EmpDept.ToList())

{

//This removes relationship from Context

dep.EmpDept.Remove(ed);

//Delete it from context

ctx.DeleteObject(ed);

}

//Delete the master table

ctx.DeleteObject(dep);

//Save to Database

ctx.SaveChanges();
}

##EasyReadMore##