C# 带有DataContext的简单SQL查询
我有一个连接到SQL Server数据库的网站,我想向其中添加一个简单的SQL查询(供管理员使用)。我希望使用DataContext,运行一个查询,然后以简单列表的形式返回结果。有没有办法做到这一点 使用C# 带有DataContext的简单SQL查询,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我有一个连接到SQL Server数据库的网站,我想向其中添加一个简单的SQL查询(供管理员使用)。我希望使用DataContext,运行一个查询,然后以简单列表的形式返回结果。有没有办法做到这一点 使用 string full_query = "SELECT " + query; IEnumerable<string> results = DB.DB().ExecuteQuery<string>(full_quer
string full_query = "SELECT " + query;
IEnumerable<string> results = DB.DB().ExecuteQuery<string>(full_query);
string full\u query=“SELECT”+查询;
IEnumerable results=DB.DB().ExecuteQuery(完整查询);
不起作用,在int出现的地方抛出错误。将模板参数更改为“object”也没有多大帮助
因此,我需要运行select语句,并将结果作为页面上的列表返回
有什么想法吗?通常您会想使用:
var results = DB.DB().SqlQuery(full_query);
如果要插入/更新/删除,可以使用:
DB.DB().ExecuteSqlCommand(full_query);
希望对您有所帮助。通常您会希望使用:
var results = DB.DB().SqlQuery(full_query);
如果要插入/更新/删除,可以使用:
DB.DB().ExecuteSqlCommand(full_query);
希望对您有所帮助。通常您会希望使用:
var results = DB.DB().SqlQuery(full_query);
如果要插入/更新/删除,可以使用:
DB.DB().ExecuteSqlCommand(full_query);
希望对您有所帮助。通常您会希望使用:
var results = DB.DB().SqlQuery(full_query);
如果要插入/更新/删除,可以使用:
DB.DB().ExecuteSqlCommand(full_query);
希望能有帮助。经过一番努力,我找到了一些有效的方法。我正在使用一个名为DatabaseResults的类来保存结果:
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>>();
}
}
公共类数据库结果
{
公共列表列名称{get;set;}
公共列表行{get;set;}
公共数据库结果()
{
ColumnNames=新列表();
行=新列表();
}
}
然后,该方法继续运行查询,获取标题并将其放入结果对象中。然后读取行,获取列值的字符串。“query”是传入的字符串。这是“select”查询,缺少select位
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();
}
DatabaseResults=newdatabaseresults();
字符串full_query=“SELECT”+查询;
DbConnection connection=DB.DB().connection;
connection.Open();
var command=connection.CreateCommand();
command.CommandText=完整查询;
尝试
{
使用(var reader=command.ExecuteReader())
{
对于(int i=0;i
我不能破坏连接,因为它被systems db对象使用,所以我需要打开和关闭它。try/catch/finally确保了这一点。经过一段时间的胡闹,我发现了一些有效的方法。我正在使用一个名为DatabaseResults的类来保存结果:
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>>();
}
}
公共类数据库结果
{
公共列表列名称{get;set;}
公共列表行{get;set;}
公共数据库结果()
{
ColumnNames=新列表();
行=新列表();
}
}
然后,该方法继续运行查询,获取标题并将其放入结果对象中。然后读取行,获取列值的字符串。“query”是传入的字符串。这是“select”查询,缺少select位
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();
}
DatabaseResults=newdatabaseresults();
字符串full_query=“SELECT”+查询;
DbConnection connection=DB.DB().connection;
connection.Open();
var command=connection.CreateCommand();
command.CommandText=完整查询;
尝试
{
使用(var reader=command.ExecuteReader())
{
对于(int i=0;i
我不能破坏连接,因为它被systems db对象使用,所以我需要打开和关闭它。try/catch/finally确保了这一点。经过一段时间的胡闹,我发现了一些有效的方法。我正在使用一个名为DatabaseResults的类来保存结果:
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>>();
}
}
公共类数据库结果
{
公共列表列名称{get;set;}
公共列表行{get;set;}
公共数据库结果()
{
ColumnNames=新列表();
行=新列表();
}
}
然后,该方法继续运行查询,获取标题并将其放入结果对象中。然后读取行,获取列值的字符串。“query”是传入的字符串。这是“select”查询,缺少select位
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();
}
Da