注意我不是這個語法的原作者,我發現
從另一個計算器的用戶,它在我的情況下工作代碼工作,在本地機器而不是在IIS
我正在IIS 7.5
和託管這個asp.net
頁面。我正在使用我的Global sax
來捕捉任何錯誤,並寫入文本文件供以後檢查(致力於實施Elmah
,但這是另一天的故事)。在IIS上,這是一臺Windows Server 2008
機器,該代碼不會引發錯誤,頁面會加載但沒有填充任何數據源。在我的本地機器上,以及其他3臺機器(全部爲非服務器)上,代碼按照它應該執行的方式執行。什麼阻止了它在我們的服務器(生產機器)上執行? GlobalAsax
void Application_Error(object sender, EventArgs e)
{
Exception CurrentException = Server.GetLastError();
Server.ClearError();
if (CurrentException != null)
{
try
{
using (StreamWriter sw = new StreamWriter(Server.MapPath("ErrorLog.txt")))
{
sw.WriteLine(CurrentException.ToString());
sw.Close();
}
}
catch (Exception ex) { }
}
}
這是我的C# -
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
Page.RegisterAsyncTask(LoadDataAsync().ToPageAsyncTask());
}
}
catch (Exception ex) { throw ex; }
}
public Task LoadDataAsync()
{
var t1 = ExecuteSqlQueryAsync(connectionString, "Select Top 10 * from uno");
var t2 = ExecuteSqlQueryAsync(connectionString, "Select Top 10 * from tres");
var t3 = ExecuteSqlQueryAsync(connectionString, "RunStoredProcedure");
return Task.Factory.ContinueWhenAll(new[] { t1, t2, t3 }, _ =>
{
try
{
this.ddluno.DataSource = t1.Result;
this.ddluno.DataBind();
ddltopperformers.DataSource = t2.Result;
ddltopperformers.DataBind();
this.gridall.DataSource = t3.Result;
}
catch (Exception exception) { throw exception; }
});
}
public System.Threading.Tasks.Task<DataSet> ExecuteSqlQueryAsync(string connectionString, string sqlQuery)
{
SqlConnection _sqlDatabaseConnection;
SqlCommand _sqlCommand = new SqlCommand();
SqlParameter _sqlParameter = new SqlParameter();
SqlDataAdapter _sqlDataAdapter = new SqlDataAdapter();
StringBuilder _sqlQueryBuilder = new StringBuilder();
DataSet _dataSet = new DataSet();
return System.Threading.Tasks.Task.Factory.StartNew(
() =>
{
try
{
_sqlDatabaseConnection = new SqlConnection(connectionString);
_sqlCommand = new SqlCommand(sqlQuery, _sqlDatabaseConnection);
_sqlDatabaseConnection.Open();
_sqlCommand.CommandTimeout = 0;
_dataSet = new DataSet();
_sqlDataAdapter = new SqlDataAdapter(_sqlCommand);
_sqlDataAdapter.Fill(_dataSet, "Data");
_sqlDatabaseConnection.Close();
_sqlCommand.Dispose();
_sqlDataAdapter.Dispose();
return _dataSet;
}
catch (Exception exception) { throw exception; }
});
}
public static class PageAsyncTaskGenerator
{
public static PageAsyncTask ToPageAsyncTask(this Task task, Action<IAsyncResult> onTimeout = null)
{
try
{
Func<Task> func =() => task;
return new PageAsyncTask(
(sender, e, cb, extraData) => func.BeginInvoke(cb, extraData),
ar => func.EndInvoke(ar).Wait(),
ar =>
{
if (onTimeout != null)
{
onTimeout(ar);
}
},
null);
}
catch (Exception exception) { throw exception; }
}
}
編輯
這是我使用的連接字符串,我應該怎麼改變,因此它適用於我的IIS?
private string connectionString = "Data Source=OnFleet;Initial Catalog=TestForCode;Integrated Security=True;MultipleActiveResultSets=True";
EDIT 2
嘗試使用webconfig
創建連接,這是我的字符串,但還沒有連接。我錯過了明顯的(再次)?
<connectionStrings>
<add name="DBConnection" connectionString="Data Source=OnFleet;Initial Catalog=TestForCode;Integrated Security=True;MultipleActiveResultSets=True;;User ID=IISUser;Password=nottellingSO" providerName="System.Data.SqlClient" />
</connectionStrings>
你改變了一個連接字符串? – Khazratbek
@Khazratbek - 沒有連接字符串是完全相同的。 – FightingApesInATub
爲什麼downvotes?我的問題在別處措辭不當或回答不正確? – FightingApesInATub