简体   繁体   中英

Simple SQL query with DataContext

I have a web-site connected to a SQL Server database, and I want to add a simple SQL query to it (for administrators). I was hoping to use the DataContext, and run a query, then return the results as a simple list. Is there any way to do this?

Using

                string full_query = "SELECT " + query;
            IEnumerable<string> results = DB.DB().ExecuteQuery<string>(full_query);

Doesn't work, throwing errors where ints come through. Changing the template parameter to "object" doesn't help much either.

So I need to run a select statement, and return the results as a list on a page.

Any ideas?

Normally you would want to use:

var results = DB.DB().SqlQuery(full_query);

If you want insert/update/delete, you can use:

DB.DB().ExecuteSqlCommand(full_query);

Hope it helps.

After a bit of messing around, I found something that works. I am using a class called DatabaseResults to hold the results:

public class DatabaseResults
{
    public List<string> ColumnNames { get; set; }
    public List<List<string>> Rows { get; set; }

    public DatabaseResults()
    {
        ColumnNames = new List<string>();
        Rows = new List<List<string>>();
    }
}

The method then goes and runs the query, grabbing the headings and putting them in the results objects. It then reads the rows, taking the strings of the column values. "query" is the string passed in. It is the "select" query, with the select bit missing.

            DatabaseResults results = new DatabaseResults();
            string full_query = "SELECT " + query;
            DbConnection connection = DB.DB().Connection;
            connection.Open();
            var command = connection.CreateCommand();
            command.CommandText = full_query;

            try
            {
                using (var reader = command.ExecuteReader())
                {

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        results.ColumnNames.Add(reader.GetName(i));
                    }

                    while (reader.Read())
                    {
                        List<string> this_res = new List<string>();
                        for (int i = 0; i < reader.FieldCount; ++i)
                        {
                            this_res.Add(reader[i].ToString());
                        }
                        results.Rows.Add(this_res);
                    }
                }
            }
            catch (Exception ex)
            {
                results.ColumnNames.Add("Error");
                List<string> this_error = new List<string>();
                this_error.Add(ex.Message);
                results.Rows.Add(this_error);
            }
            finally
            {
                connection.Close();
            }

I can't destroy the connection, as it is used by the systems db object, so I need to open and close it. The try/catch/finally makes sure this happens.

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