使用网站一段时间后,加载内容等。此消息显示“填充:SelectCommand.Connection 属性尚未初始化”!我认为这是因为 sql 连接,但不确定......我想知道我能做些什么来防止这种情况发生,每次发生这种情况我都必须上传一个文件(SQL 类,建立连接)和网站再次开始工作。
我的 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;
}
}