2015-01-01 51 views
-3

我在asp.net上做了一個登錄表單,我想從數據庫中檢索與電子郵件地址匹配的名字和姓氏。爲此我正在使用Web服務。我應該使用什麼查詢?我創建的數據庫位於Web服務上,所有網頁都在客戶端上創建。如何使用web服務從asp.net中的數據庫檢索值?

+2

需要有更多的細節才能得到答案。您正在詢問一個查詢,但沒有提供有關您的數據庫的信息。如果您通過Web服務進行此調用,則隱藏數據庫模式以支持模型,但沒有提及您正在使用的Web服務技術。你還試過了什麼?你看到什麼錯誤?所有這些都有助於更快地爲您解答問題。 – kidshaw

回答

0

,因爲我們不知道所有的細節,下面是關於如何解決你的問題的總體思路。您可以在您的Web服務中添加類似於此的代碼,前提是您有權訪問該數據庫。雖然我假設一個asmx Web服務,但同樣的想法適用於WCF。

public struct User 
     { 
      public string FirstName; 
      public string LastName; 
     } 

[WebMethod] 
public User GetUser(string emailAddress) 
{ 
    string first = string.Empty; 
    string last = string.Empty; 

    using(var connection = new SqlConnection()) 
    { 
     connection.Open(); 
     var sqlCommand = new SqlCommand(); 
     sqlCommand.CommandType = CommandType.Text; 

     // modify query to match actual table and column names 
     sqlCommand.CommandText = "select firstName, lastName from users where [email protected]"; 
     sqlCommand.Parameters.Add(new SqlParameter("@email", emailAddress)); 
     var sqlReader = sqlCommand.ExecuteReader(); 
      while(sqlReader.Read()) 
      { 
       first = sqlReader.GetValue(0).ToString(); 
       last = sqlReader.GetValue(1).ToString(); 
      } 
     } 

     // returns empty strings if no record is found 
     return new User { FirstName = first, LastName = last }; 
    } 
0

如果你問有關查詢比你可以寫一個這樣的查詢

select first_name,last_name from table where email_address='emailaddresstomatch' 
+0

和我在哪裏寫這個查詢? – Arzo

+0

在web服務中,如你所說,你正在使用web服務。 – Mairaj

相關問題