相关疑难解决方法(0)

如何轻松将DataReader转换为List <T>?

我有一个数据DataReader,我希望将其转换为List<T>.对此有什么简单的解决方案?

例如,在CustomerEntity类中,我有CustomerId和CustomerName属性.如果我的DataReader将这两列作为数据返回,那么我该如何将其转换为List<CustomerEntity>.

c# datareader generic-list

112
推荐指数
8
解决办法
26万
查看次数

从DbDataReader读取数据的最快方法是什么?

在以下代码中,command是已经设置的DbCommand:

using( var dataReader = command.ExecuteReader() /*The actual execution of the query takes relatively little time.*/ ) {
                while( dataReader.Read() ) {
                    // These are what take all of the time. Replacing them all with reader.GetValues( myArray ) has no impact.
                    val0 = dataReader.GetValue( 0 );
                    val1 = dataReader.GetValue( 1 );
                    val2 = dataReader.GetValue( 2 );
                }
            }
Run Code Online (Sandbox Code Playgroud)

我目前正在处理的查询的大部分时间都花在了GetValue调用上.它是否为每个GetValue调用进行数据库往返?看起来似乎是这样,这看起来非常低效.正如代码所指出的那样,尝试使用GetValues()一次性完成它并没有什么不同.有没有办法一次性获得整行?更好的是,有没有办法一次性获得整个结果集?

谢谢.

.net c# database

18
推荐指数
2
解决办法
3万
查看次数

什么是更好,更清洁的方式使用List <T>

我正在寻找在我正在开发的几个应用程序中实现一些更好的方法来使用List.我目前的实现看起来像这样.

MyPage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    BLL.PostCollection oPost = new BLL.PostCollection();
    oPost.OpenRecent();
    rptPosts.DataSource = oArt;
    rptPosts.DataBind();
}
Run Code Online (Sandbox Code Playgroud)

BLL班级

public class Post
{
    public int PostId { get; set; }
    public string PostTitle { get; set; }
    public string PostContent { get; set; }
    public string PostCreatedDate { get; set; }

    public void OpenRecentInitFromRow(DataRow row)
    {
        this.PostId = (int) row["id"];
        this.PostTitle = (string) row["title"];
        this.PostContent = (string) row["content"];
        this.PostCreatedDate = (DateTime) row["createddate"];
    }
}
public class PostCollection : …
Run Code Online (Sandbox Code Playgroud)

c# list

9
推荐指数
1
解决办法
355
查看次数

如何改进数据访问层选择方法模式

最近我发现自己编写数据访问层选择方法,其中代码都采用这种通用形式:

public static DataTable GetSomeData( ... arguments)
{
    string sql = " ... sql string here:  often it's just a stored procedure name ... ";

    DataTable result = new DataTable();

    // GetOpenConnection() is a private method in the class: 
    // it manages the connection string and returns an open and ready connection
    using (SqlConnection cn = GetOpenConnection())
    using (SqlCommand cmd = new SqlCommand(sql, cn))
    {
        // could be any number of parameters, each with a different type
        cmd.Parameters.Add("@Param1", SqlDbType.VarChar, 50).Value …
Run Code Online (Sandbox Code Playgroud)

.net data-access-layer .net-2.0

8
推荐指数
1
解决办法
1万
查看次数

创建数据库查询方法

我不确定是否已被删除,但我想要做的是创建一个返回查询结果的方法,以便我可以重用连接代码.据我了解,查询返回一个对象,但我如何传回该对象?我想将查询作为字符串参数发送到方法中,并让它返回结果,以便我可以使用它们.这就是我在黑暗中刺伤的东西,它显然不起作用.这个例子是我尝试使用查询结果填充列表框; 工作表名称为Employees,字段/列是名称.我得到的错误是"Complex DataBinding接受IList或IListSource作为数据源.".有任何想法吗?

 public Form1()
        {
            InitializeComponent();
            openFileDialog1.ShowDialog();
            openedFile = openFileDialog1.FileName;

            lbxEmployeeNames.DataSource = Query("Select [name] FROM [Employees$]");


        }

        public object Query(string sql)
        {
            System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
            string connectionPath;

            //build connection string
            connectionPath = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openedFile + "';Extended Properties=Excel 8.0;";

            MyConnection = new System.Data.OleDb.OleDbConnection(connectionPath);
            MyConnection.Open();
            myCommand.Connection = MyConnection;

            myCommand.CommandText = sql;
            return myCommand.ExecuteNonQuery();


        }
Run Code Online (Sandbox Code Playgroud)

c# methods return object

6
推荐指数
2
解决办法
1万
查看次数