简体   繁体   中英

Querying the row of an ADO.NET sql database given the value of its first column (C#, Xamarin)

My C# skillset is very limited. My code background is MATLAB, FORTRAN, and more recently, Python. I've used Xamarin in visual studio to bootstrap a smart device application for a quick demonstration internally.

Elsewhere in this application I create a SQLite database with ADO.NET, and populate it with entries the user selects from pickers, spinners, and text entry. Each row is a 'profile' for that person.

Now, another portion of the app takes the first column (Profile Names) and populates a spinner such that a user can select themselves thusly:

    //Check if database exists and get rider names for selection
        bool exists = File.Exists(pathToDatabase);
        string nameString = string.Empty;
        if (exists)
        {
            Console.WriteLine("Reading database");
            var connection = new SqliteConnection("Data Source=" + pathToDatabase);
            connection.Open();
            using (var contents = connection.CreateCommand())
            {
                contents.CommandText = "SELECT [Name] FROM Riders";
                var r = contents.ExecuteReader();
                while (r.Read())
                    nameString += (r["Name"].ToString() + ",");
                connection.Close();
            }
        }

        //Populate spinner with Rider Names
        string[] nameList = nameString.Split(',');
        var ridernameAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, nameList);
        ridernameAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
        RiderSelectSpinner.Adapter = ridernameAdapter;

This works great, the names appear in my spinner, and the user can select them.

The reason I'm here, is I want to now get all the rest of the data in that row associated with the profile when they select themselves, given the value of the first column (the name).

I have all the code that recognizes when they select themselves. Can someone guide me in forming a query in ADO.NET that gets the rest of the row given the first row entry? Something like below?

                contents.CommandText = "SELECT * FROM Riders WHERE [Name] = @Name"; //FIX
                contents.Parameters.AddWithValue("@Name", Name);

Thanks for your time and help.

var conn = new SQLite.Net.SQLiteConnection(plat, path); 
var resunt = conn.Table<Riders>().FirstOrDefault(x => x.Name == name); //Using LINQ or 

or

var conn = new SQLite.Net.SQLiteConnection(plat, path);     
var resunt = conn.Query<Riders>("SELECT * FROM [Riders] WHERE [Name]=" + name);

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