我連接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;
}
您是否嘗試將'de'變量設置爲mytable.Rows [0] [「SicilKod」]。ToString()? – Badiparmagi
瞭解如何解釋您的問題。 – mybirthname