2016-10-17 141 views
0

我連接SQL服務器數據庫,並使用用戶名和密碼值返回xml布爾值true。但是我想根據用戶名查詢的結果得到sicilKod的值。我該怎麼做?C#web服務從sql服務器獲取數據

該返回布爾值,根據用戶名和密碼

[的WebMethod]

public bool login(string unamePI, string passPI) 
{ 
    DataTable mytable = new DataTable(); 
    bool mydeger = false; 
    mytable = GetTableWithQueryParams("select * from UYE where USERNAME ={0} and PASSWORD={1}", unamePI, passPI); 

    if (mytable.Rows.Count > 0) 
    { 
     mydeger = true; 
    } 
    else 
    { 
     return mydeger; 
    } 
    string de = mytable.Rows[0].ToString(); 
    return mydeger; 
} 

數據庫連接:

public static DataTable GetTableWithQueryParams(string SQLCommandText, params object[] myParametres) 
{ 
    string aConnectionString = "Data Source = ..; Initial Catalog = mydatabase; Persist Security Info = True; User ID = ..; Password = .."; 
    SqlConnection SqlConn = new SqlConnection(aConnectionString); 
    SqlCommand MyCommand = new SqlCommand("", SqlConn); 
    DataTable MyTable = new DataTable(); 

    try 
    { 
     int i = 0; 
     SqlConn.Open(); 
     foreach (object MyObject in myParametres) 
     { 
      if (SQLCommandText.Contains("{" + i.ToString() + "}")) 
      { 
       SQLCommandText = SQLCommandText.Replace("{" + i.ToString() + "}", "@Prm" + i.ToString()); 
       MyCommand.Parameters.AddWithValue("Prm" + i.ToString(), MyObject); 
       i++; 
      } 
     } 
     MyCommand.CommandText = SQLCommandText; 
     SqlDataReader MyReader = MyCommand.ExecuteReader(); 
     MyTable.Load(MyReader); 
     SqlConn.Close(); 
     MyReader.Dispose(); 
    } 
    catch (Exception ex) 
    { 
     throw new Exception(SQLCommandText + "\n" + ex.Message); 
    } 
    finally 
    { 
     SqlConn.Dispose(); 
     MyCommand.Dispose(); 
    } 
    return MyTable; 
} 
+0

您是否嘗試將'de'變量設置爲mytable.Rows [0] [「SicilKod」]。ToString()? – Badiparmagi

+0

瞭解如何解釋您的問題。 – mybirthname

回答

0

改變你的方法了這一點。

public string login(string unamePI, string passPI) 
{ 
    DataTable mytable = new DataTable(); 
    string result = ""; 
    mytable = GetTableWithQueryParams("select * from UYE where USERNAME ={0} and PASSWORD={1}", unamePI, passPI); 

    if (mytable.Rows.Count > 0) 
    { 
     result = string.Format("Welcome {0}", mytable.Rows[0]["sicilKod"].ToString()); 
    } 

    return result; 

} 

然後檢查登錄方法返回值是否爲空。

+0

System.ArgumentException:'sicilKod的ü tunu,tablosuna aitdeğil。 konum:System.Data.DataRow.GetDataColumn(字符串COLUMNNAME) konum:System.Data.DataRow.get_Item(字符串COLUMNNAME) konum:LoginCpm.WebService1.login(字符串unamePI,字符串passPI) –

+0

@EmreAslan確實sicilKod在存在UYE表? – Badiparmagi

+0

是的,我有sicilKod.I從android應用程序的用戶名和密碼發送值。如果它有用戶,然後它做登錄應用程序。 –