简体   繁体   English

填充:SelectCommand.Connection属性尚未初始化

[英]Fill: SelectCommand.Connection property has not been initialized

After a while using the website, loading contents, etc.. this message shows "Fill: SelectCommand.Connection property has not been initialized"! 一段时间后使用网站,加载内容等..此消息显示“Fill:SelectCommand.Connection属性尚未初始化”! I think this is because the sql connection, but not sure... I'd like to know what can i do to prevent this, every time this happens i have to upload a file (SQL class, that make the connection) and the website starts to work again. 我认为这是因为sql连接,但不确定...我想知道我能做些什么来防止这种情况,每次发生这种情况我必须上传一个文件(SQL类,即建立连接)和网站重新开始工作。

My SQL connection: 我的SQL连接:

public class SQL
{

    SqlCommand comandos;
    public SqlConnection sql()
    {
        string Server = @"server"; 
        string Username = "user"; 
        string Password = "pass"; 
        string Database = "database";
        string ConnectionString = "Data Source=" + Server + ";";
        ConnectionString += "User ID=" + Username + ";";
        ConnectionString += "Password=" + Password + ";";
        ConnectionString += "Initial Catalog=" + Database;

        SqlConnection Connection = new SqlConnection();
        try
        {
            Connection.ConnectionString = ConnectionString;
            Connection.Open();
            return Connection;
        }
        catch (Exception)
        {
            if (Connection != null)
            {
                Connection.Dispose();
        }
        return null;
        }
    }

    public void FazerComando(string comando)
    {
        comandos = new SqlCommand(comando, sql());
        comandos.ExecuteNonQuery();
    }

    public DataTable Execute(string comando)
    {
        SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(comando, sql());
        DataTable dtResult = new DataTable();
        SQLDataAdapter.Fill(dtResult);
        return dtResult;
    }
}

This might be related to your problem, but in any case, it's something that should be addressed: You're not disposing your connections when you're done with them. 这可能与您的问题有关,但在任何情况下,都应该解决这个问题:当您完成这些连接时,您不会处置它们。 You should use using : 你应该使用using

    public void FazerComando(string comando)
    {
        using (var conn = sql())
        {
            comandos = new SqlCommand(comando, conn);
            comandos.ExecuteNonQuery();
        }
    }
    public DataTable Execute(string comando)
    {
        using (var conn = sql())
        {
            SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(comando, conn);
            DataTable dtResult = new DataTable();
            SQLDataAdapter.Fill(dtResult);
            return dtResult;
        }
    }

I've never taken that approach before. 我以前从未接受过这种方法。 We usually just use the connection string in web config, expecially with linq, it works very well. 我们通常只使用web配置中的连接字符串,尤其是linq,它运行得很好。 I suggest you have a look at http://blogs.msdn.com/b/visualstudio/archive/2012/06/11/world-of-samples-at-your-fingertips.aspx and follow the trail. 我建议您查看http://blogs.msdn.com/b/visualstudio/archive/2012/06/11/world-of-samples-at-your-fingertips.aspx并按照跟踪进行操作。 You should find a good example of a recommended best practice for connections. 您应该找到一个推荐的连接最佳实践的好例子。 The connection string will then be read at the first lauch of your app, and connection pooling (v imortant) used to best effect. 然后,您的应用程序的第一个连接将读取连接字符串,并且连接池(v imortant)用于最佳效果。

Oh and you are not disposing of your connection, which will cause a memory leek and iis to clear out your app pool when the memeory usage becomes too large -- all v bad 哦,你没有处理你的连接,这将导致内存韭菜和iis清除你的应用程序池,当记忆使用量变得太大 - 所有与坏

As the other respondant says whilst I was looking up the baet prac link... 正如另一位回应者所说的那样,我正在寻找baet prac链接......

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误填充:SelectCommand.Connection属性尚未初始化 - error Fill: SelectCommand.Connection property has not been initialized 如何解决Fill:SelectCommand.Connection属性尚未初始化 - how to solve Fill: SelectCommand.Connection property has not been initialized 填充:SelectCommand.Connection 属性尚未初始化 C# - Fill: SelectCommand.Connection property has not been initialized C# 填充:SelectCommand.Connection属性尚未初始化错误 - Fill: SelectCommand.Connection property has not been initialized error 填充:SelectCommand.Connection 属性尚未初始化 - Fill: SelectCommand.Connection property has not been initialized SelectCommand.Connection属性尚未初始化 - SelectCommand.Connection property has not been initialized 填充:SelectCommand.Connection属性尚未初始化。 该怎么办? - Fill: SelectCommand.Connection property has not been initialized. what to do? C#错误:“填充:SelectCommand.Connection属性尚未初始化。” - C# Error: “Fill: SelectCommand.Connection property has not been initialized.” 如何修复“填充:SelectCommand.Connection 属性尚未初始化”。 - How to fix 'Fill: SelectCommand.Connection property has not been initialized.' C#错误:“填充:SelectCommand.Connection属性尚未初始化。” - C# Error: “Fill: SelectCommand.Connection property has not been initialized.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM