2013年10月25日 星期五

textbox中換行字元怎麼寫...

TextBox1.Text="Hello!!你好" & vbCrLf & "請在這裡輸入你想留言的文字" & vbCrLf & "start"

2013年10月14日 星期一

此決定從簽定當天即日開始生效

Quyeát ñònh naøy coù hieäu löïc keå töø ngaøy ky
此決定從簽定當天即日開始生效

[Android] 透過WebService連接MSSQL

STP1. 建立WebService, 預設Namespace, 方法名稱SNQuery, 輸入輸出皆為string
01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Web;
05using System.Web.Services;
06using System.Data.SqlClient;
07
08namespace SNQueryWebService
09{
10    ///
11    ///Service1 的摘要描述
12    ///
13    [WebService(Namespace = "http://tempuri.org/")]
14    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
15    [System.ComponentModel.ToolboxItem(false)]
16    // 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
17    // [System.Web.Script.Services.ScriptService]
18    public class Service1 : System.Web.Services.WebService
19    {
20        [WebMethod]
21        public string HelloWorld()
22        {
23            return "Hello World";
24        }
25
26        [WebMethod]
27        public string SNQuery(string SN)
28        {
29            SqlConnection conn = new SqlConnection("server=***;database=***;uid=***;pwd=***");
30            conn.Open();
31            SqlCommand s_com = new SqlCommand();
32            s_com.CommandText = "SELECT * FROM SerialNumber WHERE SN='" + SN + "'";
33            s_com.Connection = conn;
34            SqlDataReader s_read = s_com.ExecuteReader();
35            bool result = s_read.HasRows;
36            s_read.Close();
37            conn.Close();
38            if (result)
39                return SN + " Found";
40            else
41                return SN + " NOT Found";
42        }
43    }
44}

STP2. 部屬.asmx, .asmx.cs, bin至Server, IIS>AWS>ConvertToApplication, 加.asmx為預設畫面

STP3. 下載ksoap2, 我使用的版本是2.5.2, 複製至\libs下, 並匯入Eclipse, Project>Proerties>JavaBuildPath>Libraries>AddExternalJAR..>OK

STP4. File>New>AndroidApplicationProject, 編輯res/layout/activity_main.xml如下

STP5. 編輯src/../MainActivity.java如下
01package com.example.CallWebService;
02
03import org.ksoap2.SoapEnvelope;
04import org.ksoap2.serialization.SoapObject;
05import org.ksoap2.serialization.SoapPrimitive;
06import org.ksoap2.serialization.SoapSerializationEnvelope;
07import org.ksoap2.transport.HttpTransportSE;
08
09import android.os.Bundle;
10import android.app.Activity;
11import android.view.Menu;
12import android.view.View;
13import android.widget.EditText;
14import android.widget.TextView;
15
16public class MainActivity extends Activity {
17
18    private static final String NAMESPACE = "http://tempuri.org/" ;
19    private static final String URL = "http://192.168.20.14/AWS/SNQuery.asmx"
20    private static final String SOAP_ACTION = "http://tempuri.org/SNQuery";
21    private static final String METHOD_NAME = "SNQuery";
22     
23    private EditText SNInput;
24    private TextView ResultOutput;
25
26    @Override
27    protected void onCreate(Bundle savedInstanceState) {
28        super.onCreate(savedInstanceState);
29        setContentView(R.layout.activity_main);
30        SNInput = (EditText) findViewById(R.id.SNEditText);
31        ResultOutput = (TextView) findViewById(R.id.ResultEditText);                   
32    }
33     
34    public void SNonClick(View snView) {
35        Thread networkThread = new Thread() {    
36            @Override   
37            public void run() {
38                try {
39                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
40                    request.addProperty("SN", SNInput.getText().toString());
41                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
42                    envelope.dotNet = true;//若WS有輸入參數必須要加這一行否則WS沒反應
43                    envelope.setOutputSoapObject(request);
44                    HttpTransportSE ht = new HttpTransportSE(URL);         
45                    ht.call(SOAP_ACTION, envelope);         
46                    final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();          
47                    runOnUiThread (new Runnable(){      
48                        public void run() {         
49                            ResultOutput.setText(response.toString());           
50                        }       
51                    });      
52                }catch (Exception e) {         
53                    e.printStackTrace();     
54                }    
55            }  
56        };  
57        networkThread.start();//網友說Android 3.0後網路存取必須在Thread中run
58    }
59         
60    @Override
61    public boolean onCreateOptionsMenu(Menu menu) {
62        // Inflate the menu; this adds items to the action bar if it is present.
63        getMenuInflater().inflate(R.menu.main, menu);
64        return true;
65    }
66     
67}

STP6. 於AndroidManifest.xml編輯網路存取連線
1<uses-permission android:name="android.permission.INTERNET"/>

STP7. 結果如下

REF. 原始檔