简体   繁体   中英

C# web service get data from sql server

I connected SQL server database and I using username and password value return xml boolean true. But I want to get sicilKod value according to the results of the username query. How can I do it?

This returning boolean value according to username and password

[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; 
}

Database connect:

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;
}

change your method to this.

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;

}

then check if login method return value is empty or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM